'Convert specific field to upper case by other field using sed
Using sed I need to covert to upper case the second field when the field city=miami and city=chicago
My code now looks like this, it convert all the name to upper without filtering by city.
id,name,country,sex,year,price,team,city,x,y,z
266,Aaron Russell,USA,m,1989,50,12,miami,0,0,1
179872,Abbos Rakhmonov,UZB,m,1979,0,25,chicago,0,0,0
3662,Abby Erceg,NZL,m,1977,67,20,toronto,0,0,0
5554573,Amar Music,CRO,m,1991,110,24,miami,0,0,0
3931111,Anirban Lahiri,IND,m,1987,105,27,boston,0,0,0
98402,Anissa Khelfaoui,ALG,f,1967,45,2,toronto,0,0,0
sed 's/^\(\([^,]*,\)\{1\}\)\([^,]*\)/\1\U\3/'
My output:
id,name,country,sex,year,price,team,city,x,y,z
266,AARON RUSELL,USA,m,1989,50,12,miami,0,0,1
179872,ABBOS RAKHMONV,UZB,m,1979,0,25,chicago,0,0,0
3662,ABBY ERCEG,NZL,m,1977,67,20,toronto,0,0,0,
5554573,AMAR MUSIC,CRO,m,1991,110,24,miami,0,0,0,
393115111,ANIRBAN LAHIRI,IND,m,1987,105,27,boston,0,0,0
998460252,ANISSA KHELFAOUI,ALG,f,1967,45,2,toronto,0,0,0
Expected output. Only using sed.
id,name,country,sex,year,price,team,city,x,y,z
266,AARON RUSELL,USA,m,1989,50,12,miami,0,0,1
179872,ABBOS RAKHMONV,UZB,m,1979,0,25,chicago,0,0,0
3662,Abby Erceg,NZL,m,1977,67,20,toronto,0,0,0
5554573,AMAR MUSIC,CRO,m,1991,110,24,miami,0,0,0
393115111,Anirban Lahiri,IND,m,1987,105,27,boston,0,0,0
998460252,Anissa Khelfaoui,ALG,f,1967,45,2,toronto,0,0,0
Solution 1:[1]
Easier IMHO with awk:
awk 'BEGIN{city=8; FS=OFS=","}
$city=="miami" || $city=="chicago" {$2=toupper($2)} 1' file
Prints:
id,name,country,sex,year,price,team,city,x,y,z
266,AARON RUSSELL,USA,m,1989,50,12,miami,0,0,1
179872,ABBOS RAKHMONOV,UZB,m,1979,0,25,chicago,0,0,0
3662,Abby Erceg,NZL,m,1977,67,20,toronto,0,0,0
5554573,AMAR MUSIC,CRO,m,1991,110,24,miami,0,0,0
3931111,Anirban Lahiri,IND,m,1987,105,27,boston,0,0,0
98402,Anissa Khelfaoui,ALG,f,1967,45,2,toronto,0,0,0
Solution 2:[2]
sed -E '/^([^,]*,){7}(miami|chicago),/{s/(^[^,]*,)([^,]+)/\1\U\2/}'
This relies on matching comma number 1 and number 7 in each line (ie. to match field 2 and field 8). If a single field in the CSV contained (extra) quoted or escaped commas, this would break.
Note that
\Usyntax is specific to GNU sed, and not portable.This task would probably be more clearly expressed in awk, and gawk can also handle quoted commas, using
FPAT.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dawg |
| Solution 2 | dan |
