什么命令可以按小时或按分钟计算我们看到某行的次数?
文件:
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
我想看到的输出:
按分钟:
Nov 26 08:50 2
Nov 26 08:51 5
按小时:
Nov 26 08 7
这可以通过以下方式完成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
只比较第一个n
字符。
-c
前缀行的出现次数。
awk one-liner 让您一次按小时和分钟计算:
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
测试
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]}'
输出
Nov 26 08 7
---
Nov 26 08:50 2
Nov 26 08:51 5
按小时:
awk '{split($3,a,":");b[$1" "$2" "a[1]]++}END{for(i in b)print i,b[i]}' your_file
测试如下:
> 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
>
按分钟:
awk '{split($3,a,":");b[$1" "$2" "a[1]":"a[2]]++}END{for(i in b)print i,b[i]}' your_file
测试如下:
> awk '{split($3,a,":");b[$1" "$2" "a[1]]++}END{for(i in b)print i,b[i]}' temp
Nov 26 08 7