1

我有以下文件:

1
1
2
3
3
3
4
4
5
5
5
5

我想计算一个数字出现了多少次,这个数字出现了多少次:例如数字1出现2次,数字2出现1次,数字3出现3次,数字4出现2次,5出现4次; 输出将是一个两列文件,其中第一列数字出现在列中的次数,第二列表示数字重复的次数,:

2 2  %(because the number 1 and number 4 appear 2 times and there are only 2 number that appear this often)
1 3
1 1
1 4

我希望输出的示例文件可以帮助理解......

4

2 回答 2

5

uniq需要排序的输入,因为它只比较连续的行:

uniq -c

因此,如果尚未排序:

sort | uniq -c

您给定示例的输出将是:

  2 1
  1 2
  3 3
  2 4
  4 5
于 2013-04-26T13:01:39.143 回答
2

这一行应该给你结果:

awk '{a[$0]++}END{for(x in a)b[a[x]]++;for(x in b)print b[x], x}' file

使用您的数据:

kent$  cat file
1
1
2
3
3
3
4
4
5
5
5
5

kent$  awk '{a[$0]++}END{for(x in a)b[a[x]]++;for(x in b)print b[x], x}' file
1 4
1 1
2 2
1 3
于 2013-04-26T13:20:16.950 回答