A general solution for arbitrary numbers of files:
for file1 in log1.txt log2.txt logN.txt; do
for file2 in logA.txt logB.txt logC.txt; do
for file3 in logD.txt logE.txt logF.txt; do
match1=$(tail -n 5147 $file1 | grep -c 'The line');
match2=$(grep -c "the line" $file2);
match3=$(grep -c "the line" $file3);
echo "($match1 + $match2 + $match3) * 200" | bc;
done;
done;
done
This will count the occurrences of the line
in the last 5147 lines of each of $file1
s and save that as $match1
. It will then count the occurrences of the line
in each of the $file2
s and save it as match2
and the number of occurrences in each file3
as $match3
. Then, it will calculate ($match1 + $match2 + $match3) * 200
.
Simply adding one more file to what you are doing and fixing up your syntax:
echo "$(tail -n 5147 Log1.log.2013-11-18 | grep -c 'The line') \
$(grep -c 'The line' Log2.log) $(grep -c 'The line' Log3.log)" |
awk '{print ($1+$2+$3)*200}'
There are a couple of tricks here. grep -c
counts matches instead of printing them, so no need for wc
. awk
can do math, so no need for bc
. I use echo
instead of expr
because I am not doing arithmetic in the shell anymore, just echoing the results of the three searches and passing them to awk
. This should result in something like:
echo "10 4 12" | awk '{print ($1+$2+$3)*200}'
A more concise version:
echo $((($(tail -n 5147 Log1.log.2013-11-18 | grep -c "The line) + \
$(grep -c "The line" Log2.log) + $(grep -c "The lne" Log3.log)) * 200))
This does all the math in the shell, using the $(( ))
notation. You can try it with echo $((2*4))
. It's not very easy to read, it expands to:
echo "$(( ($file1_matches + $file2_matches + $file3_matches) * 200 ))"
(number of matches in last x lines of file1 + number of matches in file2 + number of matches in fileN) * 200?