Is there a good way (pure shell, sed, awk, etc.) to "add" the log files so that I will get a summary where the format is the same, but the numbers are the sum of the numbers in each line?
I'm waiting for some shell magic Otherwise I just have to write a c/c++ program or something...
Nice elegant answer, but the array in awk does not keep the insertion order... so the resulting file does not follow the original order in the log files. Is is a way to keep track of the order as well?
If it is possible for the value of a[$1] to be 0 (e.g., $0=="counter : 0"), then "!a[$1]" would create a second entry in b for $1 the next time a "counter :..." line is encountered, which would affect the number of times the line keyed by $1 appears in the output (though the sum would be correct). Perhaps, "$1 in a" would be better.
I discovered another problem: I really want a line by line sum, and there is a problem if one of my counters' name is duplicated. So I end up with the following script (not a slick one-liner, but gets the job done):
#!/bin/zsh
num_of_lines=`grep -n -i '^=*' $1 | sed 's/:.*$//'`
for linenum in `seq $num_of_lines -1 1`; do rm file2; for i in $*; do tail -n $\
linenum $i | head -n 1 >>! file2; done && awk -F":" '{b=$1; a=a+$2} END {printf\
"%s: %12.0f\n",b,a}' file2; done