unix – 为两个字符串之间的字符串最后一次出现的日志文件

前端之家收集整理的这篇文章主要介绍了unix – 为两个字符串之间的字符串最后一次出现的日志文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个日志文件trace.log。在其中,我需要grep包含在字符串< tag>和< / tag&gt ;.这对字符串有多组,我只需要在最后一个集合之间返回内容(换句话说,从日志文件的尾部)。 额外的信用:只要内容包含“testString”,任何方式我可以返回包含在两个字符串内的内容? 谢谢你的看 编辑:搜索参数,并包含在不同的行上,大约100行内容分隔。内容是我以后…
使用tac打印文件,然后grep -m1打印一个结果。后面的外观和前瞻性检查< tag>之间的文本。和< / tag&gt ;.
  1. tac a | grep -m1 -oP '(?<=tag>).*(?=</tag>)'

测试

给定这个文件

  1. $ cat a
  2. <tag> and </tag>
  3. aaa <tag> and <b> other things </tag>
  4. adsaad <tag>and last one</tag>
  5.  
  6. $ tac a | grep -m1 -oP '(?<=tag>).*(?=</tag>)'
  7. and last one

更新

EDIT: The search parameters and are contained on different lines with
about 100 lines of content separating them. The content is what I’m
after…

那么这有点更棘手:

  1. tac file | awk '/<\/tag>/ {p=1; split($0,a,"</tag>"); $0=a[1]};
  2. /<tag>/ {p=0; split($0,"<tag>"); $0=a[2]; print; exit};
  3. p' | tac

这个想法是反转文件并使用标志p来检查< tag>已经出现了还是没有。 < / tag>将开始打印< tag>出现并完成来了(因为我们正在读书)。

> split($ 0,a,“< / tag>”); $ 0 = A [1];在< / tag>之前获取数据
>分裂($ 0,一 “<标记>” 中); $ 0 = A [2];标签>在&LT获取数据;

测试

给定一个文件是这样的:

  1. <tag> and </tag>
  2. aaa <tag> and <b> other thing
  3. come here
  4. and here </tag>
  5.  
  6. some text<tag>tag is starting here
  7. blabla
  8. and ends here</tag>

输出将是:

  1. $ tac a | awk '/<\/tag>/ {p=1; split($0,"</tag>"); $0=a[1]}; /<tag>/ {p=0; split($0,"<tag>"); $0=a[2]; print; exit}; p' | tac
  2. tag is starting here
  3. blabla
  4. and ends here

猜你在找的Bash相关文章