我目前有一个日志的输出,如下所示。
20200124_075926.795633 [24_1859] [ERROR ] [PID] error running program:
...
...
actual error message from n lines below
...
20200124_075929.261693 [24_1859] [INFO ] [PID] blah
20200124_075929.374937 [24_1859] [PERF ] [PID] blah blah
20200124_075930.660998 [24_1859] [ERROR ] [PID] some error:
20200124_075956.793528 [24_1859] [ERROR ] [PID] error running program:
...
...
actual error message from n lines below
...
我目前正在使用以下命令来输出包含错误和致命消息的日志行,去除包含时间戳记的前两列。
awk '/\[[FATAL|ERROR].*] \[.*\]/ { print substr($0, index($0,$3)) }' filename
这将产生我想要的确切结果(至少要总结计数出现的次数)。
[ERROR ] [PID] error running program:
[ERROR ] [PID] some error:
[ERROR ] [PID] error running program:
现在,我想扩展它,使其包含上面的IF中匹配的行的文本,如果该行包含第二个模式。
例如,如果第一个模式的行也包含“ error running program
”,则在下面添加第n行。否则,请继续打印。
[ERROR ] [PID] error running program: actual error message from n lines below
[ERROR ] [PID] some error:
[ERROR ] [PID] error running program: actual error message from n lines below
$ cat tst.awk
{
sub(/\r$/,"")
txt = substr($0, index($0,$3))
}
/\[(FATAL|ERROR)[^]]*] \[.*]/ {
if ( /error running program:/ ) {
pfx = txt OFS
cnt = 4
}
else {
cnt = 1
}
}
cnt && !--cnt { print pfx txt; pfx="" }
$ awk -f tst.awk file
[ERROR ] [PID] error running program: message from n lines below
[ERROR ] [PID] some error:
[ERROR ] [PID] error running program: message from n lines below
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句