2017-09-15 111 views
-2

我有一个例外文件exception.txt,其中包含昨天发生的所有异常行。如何从我的日志文件中筛选日志

例子:

Null pointer exception 
Socket exception 
Network exception 
End of file exception. 

我也有过滤文件filter.txt,其通过过滤这些线路包含了一些例外的关键字,如

End of file exception 
Null pointer exception 

我想创建一个newexception.txt文件,shell脚本在filter.txt中提及。 基本上它应该是exception.txt的副本,忽略来自filter.txt的行。

什么是最好的方法来做到这一点?

回答

0

您可以从您的过滤器文件中读取所有行,并形成从它正则表达式:

lines=`cat filter.txt` 
regex=${lines//$'\n'/|} 
egrep -v "$regex" exception.txt 

这将通过与|更换新行扁平化的过滤器行成一个字符串,那么你的正则表达式看起来像End of file exception|Null pointer exception。这样您也可以使用自己形成正则表达式的过滤器关键词,如so* exception。 注意这需要bash。

+0

这不起作用。即使给了filter.txt和exception.txt的完整路径,我也没有看到任何输出 – chandan

+0

@chandan您需要在bash shell中键入它,或者如果是脚本,则需要添加'#!/ usr/bin/bash'行。 – yacc

+0

我这样做,但仍然没有工作产生任何输出。 – chandan

1

如果你知道你有关于filter.txt的关键字。最简单,也许最快的方法是grep,使用-v选项。

grep -v -e 'keyword1' -e 'keyword2' exception.txt 
相关问题