Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use SED to convert specific column lower to upper case

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading