I have coded an analyzer which analyzes a file, and returns different results: GOOD
, BAD
, Unexpected exception :
which is followed by different exceptions... My makefile
runs this analyzer on a set of files one by one, and puts the whole result in one file output.txt
. So the output.txt
looks like as follows:
file "f1.txt"
...
GOOD
file "f2.txt"
...
Unexpected exception : exception1
...
Unexpected exception : exception2
...
Now I would like to write a shell script summary
which summarizes output.txt
, especially lists what exceptions are raised and their number of occurrence. It should like like:
exception1 : 9
exception2 : 15
...
The order of the exceptions has no importance, (well, if it is sorted by the number of the occurrence, it would be better)...
I know grep "Unexpected exception" output.txt | wc -l
will return the number of occurrence of all the exceptions, but I do need to know the occurrence for each exception raised...
Does anyone know how to write this summary
script?
Awk would probably be your best choice. I highly recommend looking at the GNU Awk User Guide for more information, as awk is extremely powerful and useful in situations like yours.
This will do what you want (a little more readable than the other answer)...
Where
i = 4
is the location of the string you want to use ($1=Unexpected $2=exception $3=: $4=exception1
. Try changing it toi = 2
and see what you get.You can use awk: