【问题标题】:how to count the repeated number on the second field in file and print the number with count of repeated [closed]如何计算文件中第二个字段上的重复数字并打印重复计数的数字[关闭]
【发布时间】:2020-08-17 18:29:58
【问题描述】:

如何计算文件中第二个字段的重复数字

并打印重复次数的数字

示例

more file

aaa 232 ....
aaa 232 ....
aaa 232
bbb 44
bbb 44
yy  20
yy 20
yy 20
yy 20
yy 20
ll 333
.
.
.

预期结果

more results

yy 5 
aaa 3  
bbb 2
ll 1
.
.
.

结果应该是从大到小

【问题讨论】:

  • 是的,但是很难开始
  • 请展示你尝试过的任何东西
  • 我从 sort -nrk 2 文件开始 | ,,, 但现在知道如何使用 awk
  • cut -d ' ' -f 1 file | uniq -c?
  • 查看问题更新

标签: linux bash perl awk sed


【解决方案1】:

使用awk,您可以这样做:

awk '{++freq[$1]} END{for (i in freq) print i, freq[i]}' file

aaa 3
ll 1
yy 5
bbb 2

如果您希望按计数降序输出,请使用此gnu awk 解决方案:

awk '{++freq[$1]} END{PROCINFO["sorted_in"] = "@val_num_desc";
for (i in freq) print i, freq[i]}' file

yy 5
aaa 3
bbb 2
ll 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 2020-04-22
    • 2017-01-08
    • 2017-08-20
    • 1970-01-01
    • 2014-11-20
    • 2021-02-16
    • 1970-01-01
    相关资源
    最近更新 更多