How to Use Awk to Convert All the Lower-Case Letters into Upper-Case

Can I use awk to convert all the lower-case letters into upper-case?

Try this:

awk '{ print toupper($0) }' <<< "your string"

Using a file:

awk '{ print toupper($0) }' yourfile.txt

Changing the case of a string with awk

The issue is that \B (zero-width non-word boundary) only seems to match at the beginning of the line, so $1 works but $2 and following fields do not match the regex, so they are not substituted and remain uppercase. Not sure why \B doesn't match except for the first field... B should match anywhere within any word:

echo 'ABCD EFGH IJKL MNOP' | awk '{for (i=1; i<=NF; ++i) { print match($i, /\B/); }}'
2 # \B matches ABCD at 2nd character as expected
0 # no match for EFGH
0 # no match for IJKL
0 # no match for MNOP

Anyway to achieve your result (capitalize only the first character of the line), you can operate on $0 (the whole line) instead of using a for loop:

echo 'ABCD EFGH IJKL MNOP' | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2)) }'

Or if you still wanted to capitalize each word separately but with awk only:

awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }'

How to replace the lower case letters to upper case letter 'C' using awk?

I would use sed for this:

sed '/^>/!s/[a-z]/C/g' file.txt

If you'd like the awk, here it is:

awk '!/^>/ { gsub(/[a-z]/, "C") }1' file.txt

Results:

>1CHE
CHKLCMCHC
>2HV3
PNMRCYCC
>5GH3
LKDCVCCQ

How to lower case part of file with awk?

why the right hand side of the equal side isn't converted to lower case?

tolower(string) function does

Return a copy of string, with each uppercase character in the string
replaced with its corresponding lowercase character. Nonalphabetic
characters are left unchanged.

So you need to assign it back to get visible effect, i.e. $2=tolower($2) rather than just tolower($2) and also set FS to OFS to avoid replacing = with i.e.

'BEGIN{FS=OFS="="}/^private/{gsub(/[-/]/, "_");$2=tolower($2)} 1'

convert to Uppercase in shell

If you want to store the result of a back in a, then you can do use command substitution:

read a;
a=$(echo $a | tr 'a-z' 'A-Z')
echo $a

Replace string in upper or lower case with Awk

Try this using gnu-awk: gawk:

echo "$sample" | awk 'BEGIN{IGNORECASE=1}{print gensub("test01", "test02", "g")}'

Output

+TEST/test02/filetest02.txt

Last chance area

echo "$sample" |
tr '[[:upper:]]' '[[:lower:]]' |
awk '{gsub("test01", "test02");print}'

Convert specific column of file into upper case in unix (without using awk and sed)

You can use tr to transform from lowercase to uppercase. cut will extract the single columns and paste will combine the separated columns again.

Assumption: Columns are delimited by tabs.

paste <(cut -f1 file) <(cut -f2 file | tr '[:lower:]' '[:upper:]')

Replace file with your file name (that is test in your case).



Related Topics



Leave a reply



Submit