Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count lines by hour

Tags:

linux

unix

awk

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
like image 993
Jola Avatar asked Nov 27 '12 10:11

Jola


2 Answers

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.

like image 143
Chris Seymour Avatar answered Sep 23 '22 08:09

Chris Seymour


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
like image 43
Kent Avatar answered Sep 24 '22 08:09

Kent