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

SED-POSIX-specified -E option inside script

I have a tricky question.

I have this .sed script where I need to do some stuff and then execute all the lines of the script with "sed -f script.sed filename.csv"

Inside the script I have used a regexp expression and if when executing the script I use -E, the script works. (sed -E -f script.sed filename.csv)

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

BUT, the thing is, I CANNOT used it, so in the terminal I need to execute the script like this "sed -f script.sed filename.csv"

Then, the question is, Is there a human possible way to make a regexp expression WORK inside a SCRIPT without using -E in the terminal? Can it be included INSIDE the SCRIPT somehow?

I will write down the script to give more context:

s/ESP/Spain/g  #this line changes ESP for Spain in all rows

s/DEN/Denmark/g  #this line changes DEN for Denmark in all rows

s/NED/Netherlands/g  #this line changes NED for Netherlands in all rows

s/^([^,]*,)([^,]+)((,[^,]*){5},(shooting|judo),)/\1\U\2\L\3/  #This line, changes to UPPER CASE all names of athletes that have as sport either judo or shooting. This is the line that does not work without the -E in the terminal.

/[0],[0],[0],$/d  #This line deletes all rows where the last 3 columns have these values

1 c id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info   #this line makes the header not upper case 

If you need some data to work with, there you have it:

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 is part of a file named athletesv2.csv

Please don’t ask why I can’t use it, but I’m forbiden from using this option.

BR,

>Solution :

You can use the first three commands as is:

s/ESP/Spain/g  #this line changes ESP for Spain in all rows
s/DEN/Denmark/g  #this line changes DEN for Denmark in all rows
s/NED/Netherlands/g  #this line changes NED for Netherlands in all rows

The fourth command should be "converted" to POSIX BRE:

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

Note that this syntax is only supported by GNU sed (the \| as an alternation operator and \+ as one or more quantifier are GNU extensions).

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