使用 -A29 进行 Grepping 可能不适用于所有情况,因为有时异常跟踪可能在ERROR行之后的行数较少,或者可能更多,具体取决于 Exception。
只需使用您提供的日志片段,就可以使用 egrep 和 regex 删除整个异常跟踪。假设 log.txt 文件具有以下记录器语句(具有良好的行以及来自异常跟踪的行):
A good line that should be captured - 1
2013-04-21 00:00:00,852 [service name] ERROR java-class - Exception
at java.net ......
at java.apache ....
A good line that should be captured - 2
2013-04-21 00:00:00,852 [service name] ERROR java-class - Exception
at java.net ......
at java.apache ....
A good line that should be captured - 3
2013-04-21 00:00:00,852 [service name] ERROR java-class - Exception
at java.net ......
at java.apache ....
A good line that should be captured - 4
2013-04-21 00:00:00,852 [service name] ERROR java-class - Exception
at java.net ......
at java.apache ....
A good line that should be captured - 5
2013-04-21 00:00:00,852 [service name] ERROR java-class - Exception
at java.net ......
at java.apache ....
要仅检索不属于异常跟踪的行,请使用以下 egrep:
egrep -vi "(error|(^\s+AT.*)|(^\s+?caused.*))" log.txt > /path/to/any/file
i : 用于忽略正则表达式中的大小写。为了证明故意将“错误”保留为小写,“AT”保留为大写。
(^\s+AT. ) :查找以空格开头的任何行,后跟“at”,后跟任何字符。
(^\s+?caused. ) :添加此正则表达式组是因为有时存在来自 Java 的嵌套堆栈跟踪,通常第一行以“Caused By”开头,然后是一些以“at .. 。”。虽然,它可以选择包含这个。
这个 egrep 的输出
A good line that should be captured - 1
A good line that should be captured - 2
A good line that should be captured - 3
A good line that should be captured - 4
A good line that should be captured - 5