A script to list all the exceptions and their numb

2019-08-06 01:51发布

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?

标签: bash shell grep
2条回答
趁早两清
2楼-- · 2019-08-06 02:33

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)...

awk '/Unexpected exception/ { for (i = 4; i <= NF; i++) freq[$i]++ } END { for (word in freq) printf "%s : %d\n", word, freq[word] }' output.txt

Where i = 4 is the location of the string you want to use ($1=Unexpected $2=exception $3=: $4=exception1. Try changing it to i = 2 and see what you get.

查看更多
一夜七次
3楼-- · 2019-08-06 02:43

You can use awk:

awk -F ' : ' '$1=="Unexpected exception"{a[$2]++} END{for (i in a) print i,a[i]}' output.txt
查看更多
登录 后发表回答