2016-03-04 101 views
-1

我想在我的/ ERROR /匹配之前打印行。要打印的行应该都包含INFO,直到找到前一个ERROR。awk打印行之前匹配INFO直到匹配错误

所以,如果我有一个文件

ERROR this is an error 
INFO error found on line 2 
INFO error is due to something 
ERROR this is another error 

我想从ERROR this is another error的/ ERROR /打印

INFO error found on line 2 
INFO error is due to something 
ERROR this is another error 

任何人都知道吗?我当前脚本的

部分:

/CRITICAL/ { 
    print "\x1b[93;1m" 
} 
/ERROR/  { 
    print "\x1b[37m" 
} 
/ERROR|EMERGENCY|CRITICAL/ { 
    if (NR == n+1) print ""; 
    n = NR; 
    print x;print 
    print "\x1b[0m" 
};{x=$0}' 
+0

编辑您的问题提供了大量样本输入集,包括你不想匹配的行。现在,根据文件的其他部分包含什么以及您想要做什么,可能会有太多答案。例如,如果在输出段之间是否存在空白行,是否存在第三个ERROR行?不要在评论中回答 - 编辑您的问题以显示更全面的输入/输出。 –

回答

0

试试这个内胆: awk 'x;/ERROR/{x=1}' file

日期:

INFO error found on line 2 
INFO error is due to something 
ERROR this is another error 

龙版本:

x;/ERROR/{ 
    x1=1; 
    print 
} 

如果找到“错误”x = 1,如果x为真,我们已经通过该行,然后我们打印,直到我们再次通过该行。

或者,也许这个,我没有非常清楚你需要什么输出。

awk '/ERROR/{x=1;next}/ERROR/{x=1}x'

INFO error found on line 2 
INFO error is due to something