For example, you have a file which contains log of some events.
Input data:
Dec 9 00:33:42 some log
Dec 9 00:56:49 some log
Dec 9 01:13:12 some log
Dec 9 01:22:02 some log
Dec 9 01:35:52 some log
Dec 9 03:15:52 some log
Dec 9 12:17:52 some log
And you want to group this file by hours. The code below will help group and display data:
grep -oP "Dec\s+9\s(\d{2})" | sort | uniq -c
Output data:
2 Dec 9 00
3 Dec 9 01
1 Dec 9 03
1 Dec 9 12
First column - it's count of unique lines with the date.
Let's break down the command:
- Flag
-o
is required to output only the requested substring(Dec\s+9\s(\d{2}))
- Flag
-P
is required for enable perl-style regexes
The pipeline works as follows:
- Command
sort
sorts the data - And
uniq -c
finds count of unique lines
Thank you for reading. Best regards, Ildar.