使用sed或awk检索两个字符串之间的值

时间:2021-12-23 15:30:55

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.

非常感谢你的帮助,AL。

2 个解决方案

#1


2  

You can use the following sed command:

您可以使用以下sed命令:

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

These are two commands, separated by a semicolon:

这是两个命令,用分号分隔:

  • s/time=// removes the time= prefix
  • s / time = //删除time =前缀
  • s/<\/*DISP>/ /g removes the <DISP> or </DISP> tags by a space
  • s / <\ / * DISP> / / g用空格删除 或 标记

#2


0  

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

一个不同的aproach选择匹配而不是删除不想要的字符串:

$ 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

#1


2  

You can use the following sed command:

您可以使用以下sed命令:

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

These are two commands, separated by a semicolon:

这是两个命令,用分号分隔:

  • s/time=// removes the time= prefix
  • s / time = //删除time =前缀
  • s/<\/*DISP>/ /g removes the <DISP> or </DISP> tags by a space
  • s / <\ / * DISP> / / g用空格删除 或 标记

#2


0  

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

一个不同的aproach选择匹配而不是删除不想要的字符串:

$ 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