count lines by hour

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

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.


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

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

Need Your Help

ASP.NET Core API only returning first result of list

c# json entity-framework asp.net-web-api asp.net-core

I have created a teams web api controller and trying to call the GET method to get the json result of all the teams in the database. But when I make the call I am only getting the first team back i...