Home > database >  Use SED to convert specific column lower to upper case
Use SED to convert specific column lower to upper case

Time:11-25

I must to convert the 4 column to upper case with only one sed command.

user,gender,age,native_lang,other_lang
0,M,19,finnish,english swedish german 
1,M,30,urdu,english 
2,F,26,finnish,english swedish german
3,M,20,finnish,english french swedish 
4,F,20,finnish,english swedish 

This it's my best movement but change all columns.

sed -e 's/\(.*\)/\U\1/' 

I really want understand the command, but I really lose myself why use" \ " or how to read it.

CodePudding user response:

You can use

sed 's/^\(\([^,]*,\)\{3\}\)\([^,]*\)/\1\U\3/' # POSIX BRE
sed -E 's/^(([^,]*,){3})([^,]*)/\1\U\3/'      # POSIX ERE

See an online demo:

s='user,gender,age,native_lang,other_lang
0,M,19,finnish,english swedish german 
1,M,30,urdu,english 
2,F,26,finnish,english swedish german
3,M,20,finnish,english french swedish 
4,F,20,finnish,english swedish '
sed 's/^\(\([^,]*,\)\{3\}\)\([^,]*\)/\1\U\3/' <<< "$s"

Output:

user,gender,age,NATIVE_LANG,other_lang
0,M,19,FINNISH,english swedish german 
1,M,30,URDU,english 
2,F,26,FINNISH,english swedish german
3,M,20,FINNISH,english french swedish 
4,F,20,FINNISH,english swedish 

Details:

  • ^ - start of string
  • (([^,]*,){3}) - Group 1: three repetitions of any zero or more chars other than a comma and then a comma
  • ([^,]*) - Group 3: zero or more chars other than a comma.

CodePudding user response:

This might work for you (GNU sed):

sed 's/[^,]*/\U&/4' file

Uppercase the fourth set of non-commas.


If not the headers, use:

sed '1!s/[^,]*/\U&/4' file
  • Related