如何计算文件中重复的数字,然后重复组织它们?

时间:2015-10-23 20:36:37

标签: bash awk

我必须制作一个bash脚本,在文本文件中计算1到45的所有数字,并显示每个数字的重复次数,从最重复到最少排序。

示例:file.txt

7   22  22   7  13  31
13  13   7  13  13  40

输出:output.txt

13 = 5
7 = 3
22 = 2
31 = 1
40 = 1

4 个答案:

答案 0 :(得分:3)

tr -s " " "\n" | sort | uniq -c | sort -n r | awk '{print $2 " = " $1}'
# each item in |     count      |    sort   |   fix output format
#  a new line

答案 1 :(得分:2)

使用GNU awk进行多字符RS\s简写[[:space:]]并排序in

$ awk -v RS='\\s+' '{cnt[$0]++} END{PROCINFO["sorted_in"]="@val_num_desc"; for (i in cnt) print i" = "cnt[i]}' file
13 = 5
7 = 3
22 = 2
31 = 1
40 = 1

答案 2 :(得分:1)

$ tr -s ' ' '\n' <file | sort | uniq -c | sort -nr | awk '{print $2 " = " $1}'
13 = 5
7 = 3
22 = 2
40 = 1
31 = 1

如果需要对数值进行额外排序,则

$ tr -s ' ' '\n' <nums | sort | uniq -c | sort -k1,1nr -k2n | awk '{print $2 " = " $1}'
13 = 5
7 = 3
22 = 2
31 = 1
40 = 1

答案 3 :(得分:1)

这个awk单行应该一次性给出你的报告:

awk -v RS=" +|\n" '{a[$1]++}END{for(x in a)printf "%d = %d\n",x,a[x]}' file

如果要按频率降序对输出进行排序,只需将输出传递给sort -nr -k3

使用您的数据进行测试:

kent$  awk -v RS=" +|\n" '{a[$1]++}END{for(x in a)printf "%d = %d\n",x,a[x]}' f|sort -nr -k3
13 = 5
7 = 3
22 = 2
31 = 1
40 = 1