是否可以告诉grep为每种类型的匹配搜索字符串使用不同的输出文件?
我需要递归搜索所有* .log以查找所有" [错误]"," [警告]"和" [ASSERT]"。但我想将它分隔在不同的输出文件中。每个搜索字符串一个输出文件。 (没有多次调用grep!)
搜索已经有效:(从gmake调用)
grep -nHr --include=*.log -e '\[ERROR\]' -e '\[WARN\]' -e '\[ASSERT\]' $(root_path) > $(root_path)/result.txt
但是由于性能原因,我不想多次调用grep:
grep -nHr --include=*.log -e '\[ERROR\]' $(root_path) > $(root_path)/result_[ERROR].txt
grep -nHr --include=*.log -e '\[WARN\]' $(root_path) > $(root_path)/result_[WARN].txt
是否有可能以某种方式:
grep -nHr --include=*.log -e {'PATTERN1' | 'PATTERN2' | 'PATTERN3'} $(root_path) > $(root_path)/result{'PATTERN1'|'PATTERN2'|'PATTERN3'}.txt
答案 0 :(得分:2)
要通过1遍传递文件,并写入多个outFiles,我会使用awk
awk '/^\[Error\]/ {print FILENAME ":" NR ":" $0 > "errorFile.txt" }
/^\[Warn\]/ {print FILENAME ":" NR ":" $0 > "warnFile.txt" }
/Assert/ {print FILENAME ":" NR ":" $0 > "assertFile.txt" }' logFile
如果您确定错误/警告/断言令牌始终是感兴趣的行中的第一个并且使用行首的reg-exp字符{{1>,则可以略微提高速度。 }}),即。
^
答案 1 :(得分:1)
sed -n '{
/\[ERROR\]/w errorlog
/\[WARN\]/w warnlog
/\[ASSERT\]/w assertlog
}' $( find /your/path/here -type f -name "*.log" )
可能适合你。