假设我有一个包含三列的文件,如下所示:
00:00:01 Login Steve
00:00:01 Install Sarah
00:00:01 Install Sarah
00:00:02 Explorer Sarah
00:00:02 Explorer Sarah
00:00:02 Install Steve
00:00:02 Firewall Sarah
00:00:02 Logout Steve
00:00:04 Logout Sarah
是否可以使用awk来计算每个用户在每个时间戳中执行的唯一操作,因此输出如下所示:
00:00:01 Steve Login 1
00:00:01 Sarah Install 2
00:00:02 Sarah Explorer 2
00:00:02 Steve Install 1
00:00:02 Sarah Firewall 1
00:00:02 Steve Logout
00:00:04 Sarah Logout
这是我最接近的:
awk '{count[$1,$3,$2]++}END{for (i in count){split(i,a,SUBSEP); print a[1],a[2],count[i]}}' awktest.txt
这给了我这个结果:
00:00:02 Sarah 1
00:00:02 Steve 1
00:00:02 Steve 1
00:00:01 Steve 1
00:00:04 Sarah 1
00:00:02 Sarah 2
00:00:01 Sarah 1
00:00:01 Sarah 1
我在Cygwin这样做。
答案 0 :(得分:0)
$ awk -F"\t" -v OFS="\t" '{arr[$0]+=1} END {for(i in arr) print i,arr[i]}' test.in
00:00:01 Install Sarah 2
00:00:04 Logout Sarah 1
00:00:02 Firewall Sarah 1
00:00:01 Login Steve 1
00:00:02 Logout Steve 1
00:00:02 Install Steve 1
00:00:02 Explorer Sarah 2