I want to grep the string which comes after WORK=
and ignore if there comes paranthesis after that string .
The text looks like this :
//INALL TYPE=GH,WORK=HU.ET.ET(IO) //INA2 WORK=HU.TY.TY(OP),TYPE=KK //OOPE2 TYPE=KO,WORK=TEXT.LO1.LO2,TEXT //OOP2 TYPE=KO,WORK=TEST1.TEST2 //H1 WORK=OP.TEE.GHU,TYPE=IU
So, desirable output should print only :
TEXT.L01.L02 TEST1.TEST2 OP.TEE.GHU
So far , I could just match and cut before WORK=
but could not remove WORK=
itself:
sed -E 's/(.*)(WORK=.*)/2/'
I am not sure how to continue . Can anyone help please ?
Advertisement
Answer
You can use
sed -n '/WORK=.*([^()]*)/!s/.*WORK=([^,]*).*/1/p' file > newfile
Details:
-n
– suppresses the default line output/WORK=.*([^()]*)/!
– if a line contains aWORK=
followed with any text and then a(...)
substring skips its/.*WORK=([^,]*).*/1/p
– else, takes the line and removes all up to and includingWORK=
, and then captures into Group 1 any zero or more chars other than a comma, and then remove the rest of the line;p
prints the result.
See the sed
demo:
s='//INALL TYPE=GH,WORK=HU.ET.ET(IO) //INA2 WORK=HU.TY.TY(OP),TYPE=KK //OOPE2 TYPE=KO,WORK=TEXT.LO1.LO2,TEXT //OOP2 TYPE=KO,WORK=TEST1.TEST2 //H1 WORK=OP.TEE.GHU,TYPE=IU' sed -n '/WORK=.*([^()]*)/!s/.*WORK=([^,]*).*/1/p' <<< "$s"
Output:
TEXT.LO1.LO2 TEST1.TEST2 OP.TEE.GHU