user2335924
user2335924

Reputation: 55

Retrieve value between two string using sed or awk

I am trying to retrieve value between two strings which are present multiple time in one single line.

here is what I got:

time="1441491171" <DISP>something</DISP><DISP>stuff</DISP><DISP>possible</DISP>

the order for these strings as it might change by having additional strings...

I am trying to get these values are below:

"1441491171" something stuff possible

Many thanks for you help, AL.

Upvotes: 0

Views: 57

Answers (2)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

A different aproach selecting matches instead of deleting not wanted strings:

$ grep -oP 'time=\K"\d+"|(?<=DISP>)\w+(?=</DISP)' file
"1441491171" 
something
stuff
possible

$ grep -oP 'time=\K"\d+"|(?<=DISP>)\w+(?=</DISP)' file |tr  '\n' ' '
"1441491171"  something stuff possible

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158010

You can use the following sed command:

sed 's/time=//;s/<\/*DISP>/ /g'

These are two commands, separated by a semicolon:

  • s/time=// removes the time= prefix
  • s/<\/*DISP>/ /g removes the <DISP> or </DISP> tags by a space

Upvotes: 2

Related Questions