bw使用awk或sed从出现的位置向后搜索到特定的字符串 [英] bash using awk or sed to search backwards from occurance to a specific string

查看:30
本文介绍了bw使用awk或sed从出现的位置向后搜索到特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,正在此文件中搜索字符串.一旦(如果找到)字符串,我需要能够搜索到另一个字符串的位置并输出数据.

I've an xml file and am searching for a string in this file. Once (and if) the string is found I need to be able to search back to the position of another string and output the data.

即:

<xml>
<packet>
 <proto>
 <field show="bob">
 </proto>
</packet>
<packet>
 <proto>
 <field show="rumpelstiltskin">
 </proto>
</packet>
<packet>
 <proto>
 <field show="peter">
 </proto>
</packet>

我的输入将被知道:

show="rumpelstiltskin" 

<packet>

我需要得到以下结果(基本上是第二个块);

I need to get the following result (which is basically the second block);

<packet>
<proto>
<field show="rumpelstiltskin">
</proto>
</packet>

<packet>
<proto>
<field show="rumpelstiltskin">

成千上万的(wireshark pdml转换),并且show ="rumpelstilstkin"可以出现在文件中的任何位置,并且该节可以是任意大小.

There are thousands of (wireshark pdml conversion) and the show="rumpelstilstkin" can occur anywhere in the file and the section can be of any arbitrary size.

我以前已经这样做过,并且非常确定有可能使用awk或sed oneliner ..任何帮助表示赞赏!

I've done this before and am pretty sure it's possible in an awk or sed oneliner.. any help appreciated!

推荐答案

所以...您可能会一起破解某些东西,从而将文件作为文本文件进行基本解析...

So ... you COULD hack something together that would do basic parsing of your file as a text file...

awk -v txt="rumpel" '$0=="<packet>"{s=$0; found=0; next} $0~txt{found=1} {s=s RS $0} $0=="</packet>" && found {print s}' inp.xml

为了便于说明,将其分解为多个部分,以进行以下操作:

Broken out into pieces for easier explanation, this does the following:

  • -v txt ="rumpel" -设置在脚本内使用的变量.请注意,在此示例中,它将作为正则表达式进行评估,但是如果您希望将其搜索为字符串,则可以使用 index().
  • $ 0 ==< packet>" {s = $ 0;找到= 0;next} -如果找到数据包的开头,请重置存储变量( s )和标志( found ).
  • $ 0〜txt {found = 1} -如果找到所需的文本,请设置一个标志.
  • {s = s RS $ 0} -将当前行附加到变量,然后
  • $ 0 ==</packet>"&&找到了{print s} -如果我们位于文本的末尾,并且找到了字符串,请打印.
  • -v txt="rumpel" - sets a variable for use within the script. Note that this will be evaluated as a regex in this example, but you could use index() if you prefer to search for it as a string.
  • $0=="<packet>"{s=$0; found=0; next} - If we find the start of a packet, reset our storage variable (s) and flag (found).
  • $0~txt{found=1} - If we find the text we're looking for, set a flag.
  • {s=s RS $0} - Append the current line to a variable, and
  • $0=="</packet>" && found {print s} - if we're at the end of our text and the string was found, print.

一种更好的方法可能是使用一种本机理解XML的东西来解释XML,但是仅使用sed和awk不可能.

A better approach would likely be to interpret the XML using something that understands XML natively, but that isn't possible with just sed and awk.

这篇关于bw使用awk或sed从出现的位置向后搜索到特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆