我在日志文件中有以下内容,
01:31:01222接收到的事件
01:31:01435接收到的事件
01:31:01441接收到的事件
01:31:01587接收到的事件
01:31:02110接收到的事件
01:31:02650接收到的事件
01:31:02869接收到的事件
01 :31:03,034 接收事件
01:31:03,222 接收事件
我想按秒分组并计算每组中的行数以输出以下内容,
01:31:01 4 01:31:02 3
01:31:03 2
理想情况下,我喜欢在一个简单的 awk 脚本中执行此操作,而不必求助于 perl 或 python,有什么想法吗?谢谢。
听起来像是一份工作awk
:
awk -F, '{a[$1]++}END{for(i in a){print i, a[i]}}' file.txt
Run Code Online (Sandbox Code Playgroud)
输出:
awk -F, '{a[$1]++}END{for(i in a){print i, a[i]}}' file.txt
Run Code Online (Sandbox Code Playgroud)
解释:
我正在使用选项-F
(字段分隔符)并将其设置为,
. 这使得在字段 1 ( $1
) 中获得秒精度的时间变得容易。
脚本本身的解释(以多行形式):
# Runs on every line and increments a count tied to the first field (the time)
# (The associative array a will get created on first access)
{a[$1]++}
# Runs after all lines have been processed. Iterates trough the array 'a' and prints
# each key (time) and its associated value (count)
END {
for(i in a){
print i, a[i]
}
}
Run Code Online (Sandbox Code Playgroud)