2016-03-04 100 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