grep:搜索一次不同的模式,并输出到几个文件 [英] grep: search once for different pattern and output to several files
问题描述
是否有可能告诉grep使用不同的输出文件为每个类型的匹配搜索字符串?
我需要搜索所有* .log递归查找所有 [错误],[警告]和[ASSERT]。但我想分开在不同的输出文件。每个搜索字符串一个输出文件。 (没有调用grep几次!)
搜索已经工作:(从gmake调用)
grep -nHr --include = *。log -e'\ [ERROR \]'-e'\ [WARN\]'-e'\ [ASSERT\]'$ (root_path)> $(root_path)/result.txt
但由于性能,我不想多次调用grep :
$ $ p $ 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
通过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 char( ^
),即
/ ^错误/ {print $ 0> errorFile.txt
Is it possible to tell grep to use different output files for every type of matching search string?
I need to search all *.log recursively to find all "[ERROR]", "[WARN]" and "[ASSERT]". But I would like to have it separated in different output files. One output file for each search string. (without calling grep several times!)
searching already works: (called from gmake)
grep -nHr --include=*.log -e '\[ERROR\]' -e '\[WARN\]' -e '\[ASSERT\]' $(root_path) > $(root_path)/result.txt
But due to performance I do not want to call grep several times:
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
Is it possible to have it somehow like:
grep -nHr --include=*.log -e {'PATTERN1' | 'PATTERN2' | 'PATTERN3'} $(root_path) > $(root_path)/result{'PATTERN1'|'PATTERN2'|'PATTERN3'}.txt
To read thru the file in 1 pass, and write to multiple outFiles, I would use awk
awk '/^\[Error\]/ {print FILENAME ":" NR ":" $0 > "errorFile.txt" }
/^\[Warn\]/ {print FILENAME ":" NR ":" $0 > "warnFile.txt" }
/Assert/ {print FILENAME ":" NR ":" $0 > "assertFile.txt" }' logFile
You can gain a minor increase in speed, if you know for certain that the Error/Warn/Assert tokens are always the first on the line of interest and use the beginning-of-line reg-exp char (^
), ie.
/^Error/ {print $0 > "errorFile.txt"
这篇关于grep:搜索一次不同的模式,并输出到几个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!