Reputation: 313
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
Upvotes: 20
Views: 11069
Reputation: 67231
By hour:
awk '{split($3,a,":");b[$1" "$2" "a[1]]++}END{for(i in b)print i,b[i]}' your_file
tested Below:
> awk '{split($3,a,":");b[$1" "$2" "a[1]":"a[2]]++}END{for(i in b)print i,b[i]}' temp
Nov 26 08:50 2
Nov 26 08:51 5
>
By minute:
awk '{split($3,a,":");b[$1" "$2" "a[1]":"a[2]]++}END{for(i in b)print i,b[i]}' your_file
tested below:
> awk '{split($3,a,":");b[$1" "$2" "a[1]]++}END{for(i in b)print i,b[i]}' temp
Nov 26 08 7
Upvotes: 2
Reputation: 195079
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
Upvotes: 2
Reputation: 85795
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.
Upvotes: 35