如何计算一列的内容由两个其他与awk? [英] How to count the contents of one column by two others with awk?
本文介绍了如何计算一列的内容由两个其他与awk?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个包含以下三列的文件:
Say I have a file with three columns, as follows:
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来计算每个用户在每个时间戳中执行的唯一操作,所以输出结果如下:
Is it possible to use awk to count up the unique actions each user performs in each time stamp, so the output is something like this:
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 。
推荐答案
$ 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
这篇关于如何计算一列的内容由两个其他与awk?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文