Home > Software design >  SED- Change column from lower to upper case based on 2 conditions from script
SED- Change column from lower to upper case based on 2 conditions from script

Time:04-19

I have to change column "name" from lower to upper case based on the condition that the sport column is either shooting or judo.

So far, I have the following code provided by anubhava works in the terminal, but not inside the script:

sed -E 's/^([^,]*,)([^,] )((,[^,]*){5},(shooting|judo),)/\1\U\2\L\3/'

Some input:

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,Abdelraouf Benguit,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,Abdullah Alrashidi,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,Abdullah Hel Baki,BAN,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

Expected output:

    id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
    388896171,ABDELRAOUF BENGUIT,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
    285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
    545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
    969824503,ABDULLAH ALRASHIDI,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
    897549624,ABDULLAH HEL BAKI,BAN,male,1989-08-01,,,shooting,0,0,0,
    153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

This code will be going into a script and executed with sed -f. It needs to be with sed.

The script is basically the following lines:

s/ESP/Spain/g
s/DEN/Denmark/g
s/NED/netherlands/g
s/^([^,]*,)([^,] )((,[^,]*){5},(shooting|judo),)/\1\U\2\L\3/
/[0],[0],[0],$/d
1 c id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info

And I'm calling the script like this:

$ sed -f script3_3.sed athletesv22.csv

Thanks,

CodePudding user response:

sed is not the right tool for this. You should consider awk.

You may use a awk solution like this:

awk 'BEGIN {FS=OFS=","} NR > 1 && $8 ~ /^(shooting|judo)$/ {
$2 = toupper($2)} 1' file.csv

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,ABDELRAOUF BENGUIT,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,ABDULLAH ALRASHIDI,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,ABDULLAH HEL BAKI,BAN,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

To save changes inline to input file use this gnu-awk solution:

awk -i inplace 'BEGIN {FS=OFS=","}
NR > 1 && $8 ~ /^(shooting|judo)$/ {$2 = toupper($2)} 1' file.csv

Or if you don't have gnu-awk then use:

awk 'BEGIN {FS=OFS=","} NR > 1 && $8 ~ /^(shooting|judo)$/ {
$2 = toupper($2)} 1' file.csv > _tmp_ && mv _tmp_ file.csv

This gnu-sed solution should work for you:

sed -E 's/^([^,]*,)([^,] )((,[^,]*){5},(shooting|judo),)/\1\U\2\L\3/' file.csv

id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,ABDELRAOUF BENGUIT,alg,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,ABDULLAH ALRASHIDI,ioa,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,ABDULLAH HEL BAKI,ban,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,

Working Demo

  • Related