提取物对模式的任何一端由一个标识符封闭的多条线路 [英] Extract multiple lines on either end of pattern which are enclosed by an identifer

查看:110
本文介绍了提取物对模式的任何一端由一个标识符封闭的多条线路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从日志文件中提取特定事件的痕迹。为了找到相关的事件,我找了$模式$。要提取该事件​​的完整的曲线,我期待在由$ $ SEPARATOR包围的图案两端提取线

I am trying to extract the trace of a specific event from log files. To find the relevant event, I look for a $PATTERN$. To extract the complete trace of the event, I am looking to extract lines on either end of the pattern enclosed by a $SEPARATOR$

例如,如果日志文件的内容是

For example, if the contents of log file is

Line1
Line2
SEP
Line3
Line4
Name=PATTERN
Line5
SEP
Line 6
...

我要提取

SEP
Line3
Line4
Name=PATTERN
Line5
SEP

我试图用sed和免费得到它的单行工作如下一致:

I tried to use sed and got it working for single line matches as below:

echo "randomStringSEPrandomPATTERNrandomSEPrandom" | sed -n 's/^.*\(SEP.*PATTERN.*SEP\).*/\1/p'

收益 SEPrandomPATTERNrandomSEP

如何将其扩展为多行任何帮助将是非常美联社preciated。谢谢你。

Any help on how to extend it for multiple lines would be much appreciated. Thanks.

推荐答案

这是不是 SED 一个很自然的事。使用 AWK 代替。

This is not a very natural task for sed. Use awk instead.

A GAWK 特异性的版本(感谢Jotne更正):

A gawk-specific version (thanks Jotne for corrections):

gawk -vRS="SEP" '/PATTERN/ {print RT $0 RT}'

一个对POSIX awk的版本。应在BSD / OSX工作。

A version for POSIX awk. Should work on BSD/OSX.

awk '
  /SEP/ {
    out = out $0 "\n"
    if (in_seps == 1) {
      if (pattern_found) {
        printf(out)
        pattern_found = 0
      }
      in_seps = 0
      out = ""
    } else
      in_seps = 1
    next
  }

  in_seps == 1 {
    out = out $0 "\n"
  }

  /PATTERN/ {
    pattern_found = 1
  }
'

A 的sed 脚本。使用GNU扩展T(如T,但相反的情况)。

A sed script. Uses GNU extension T (like t but opposite condition).

sed -n '
  H               # append line to holdspace
  /SEP/ {         # if line was a separator
    x             # exchange pattspace and holdspace
    s/^SEP/&/     # check if it begins with a separator
    T             # if it doesn't, go to next line
    s/PATTERN/&/  # check if it contains the pattern
    T             # if it doesn't, go to next line
    p             # print it
  }
'

这篇关于提取物对模式的任何一端由一个标识符封闭的多条线路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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