I had a log dir where one of my applications used to dump logs. The number of files in the dir is more than 1000's and I had to count the number of files been modified in each month. This is a one liner for the same.
$ ls -l | sed 1d | awk 'BEGIN{print "Month (Num Files)"}{count[$6]++}END{for(j in count) print j,"("count[j]")"}'
Month (Num Files)
Feb (100)
Apr (341)
Jan (921)
Mar (343)
The same logic can be used to count the number of occurrences of a particular field in a file. e.g.
$ cat myfile
1
1
3
3
3
3
5
6
6
6
If one had to calculate how many times each of the digits occurred.
$ awk 'BEGIN{print "Number (count)"}{count[$1]++}END{for(j in count) print j,"("count[j]")"}' myfile
Output:
Number (count)
5 (1)
6 (3)
1 (2)
3 (4)
No comments:
Post a Comment