计算
我们按小时或按分钟看到某行的次数的命令是什么?
文件:
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 单行代码一次为您提供小时和分钟计数:
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