Reputation: 153
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
Upvotes: 1
Views: 208
Reputation: 21965
sed -n '{
/\[ERROR\]/w errorlog
/\[WARN\]/w warnlog
/\[ASSERT\]/w assertlog
}' $( find /your/path/here -type f -name "*.log" )
may work for you.
Upvotes: 1
Reputation: 37278
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"
Upvotes: 2