使用awk组合频率列表



我想合并两个频率列表,频率应该聚合。我注意到有几个类似的线程,但我想讨论我的方法,以及,如果这是可以的?

所以这是我在cygwin中尝试做的:

我收集了一些我在网上找到的推荐,最终使用了sort uniq和awk,这对我来说似乎很有用。我尝试了几种方法,但我只展示第一种和最后一种:

sort testcf.txt | uniq -c  | awk '{  print $2 + $3 "t" $1 }' > testcf-sorted.txt
...
sort testcf.txt | uniq -c  | awk '{  print $2 "t" $1 }' > testcf-sorted.txt

列表如下:

foo 1
bar 3
foo 2
fnord 2
foo 1
fnord 2

我想接收:

bar 3
fnord 4
foo 4

但是我得到:

第一种方法:

3   1
2   1
2   1
1   2
2   1
去年的方法:

bar 1
fnord   1
fnord   1
foo 2
foo 1

如果您使用sort和uniq,并且您的输入包含两次"foo 1",则其中一个将丢失。我想你只是在找:

awk '{a[$1] += $2} END {for( i in a ) print i, a[i]}' testcf.txt

最新更新