What would be the command to count how many times we saw certain line by hour or by minute?
File:
Nov 26 08:50:51
Nov 26 08:50:51
Nov 26 08:51:09
Nov 26 08:51:09
Nov 26 08:51:09
Nov 26 08:51:09
Nov 26 08:51:40
Output I would like to see:
by minute:
Nov 26 08:50 2
Nov 26 08:51 5
by hour:
Nov 26 08 7
This can be done with uniq
:
$ uniq -w9 -c file # by hour
7 Nov 26 08:50:51
$ uniq -w12 -c file # by minute
2 Nov 26 08:50:51
5 Nov 26 08:51:09
-w
compare no more than the first n
characters.
-c
prefix lines by the number of occurrences.
the awk one-liner gives you count by hour and min in one shot:
awk -F: '{h[$1]++;m[$1":"$2]++;}END{for(x in h)print x,h[x]; print "---"; for(x in m)print x,m[x]}' file
test
kent$ echo "Nov 26 08:50:51
Nov 26 08:50:51
Nov 26 08:51:09
Nov 26 08:51:09
Nov 26 08:51:09
Nov 26 08:51:09
Nov 26 08:51:40"|awk -F: '{h[$1]++;m[$1":"$2]++;}END{for(x in h)print x,h[x]; print "---"; for(x in m)print x,m[x]}'
output
Nov 26 08 7
---
Nov 26 08:50 2
Nov 26 08:51 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With