bash 如何排序、uniq 和显示出现超过 X 次的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20147878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:43:26  来源:igfitidea点击:

How to sort,uniq and display line that appear more than X times

bashsortinguniq

提问by Andrew Kennen

I have a file like this:

我有一个这样的文件:

80.13.178.2
80.13.178.2
80.13.178.2
80.13.178.2
80.13.178.1
80.13.178.3
80.13.178.3
80.13.178.3
80.13.178.4
80.13.178.4
80.13.178.7

I need to display unique entries for repeated line (similar to uniq -d) but only entries that occur morethan just twice (twice being an example so flexibility to define the lower limit.)

我需要显示重复行的唯一条目(类似于 uniq -d),但只显示出现两次以上的条目(两次作为示例,因此可以灵活地定义下限。)

Output for this example should be like this when looking for entries with three or more occurrences:

在查找出现三个或更多的条目时,此示例的输出应如下所示:

80.13.178.2
80.13.178.3

采纳答案by hek2mgl

With pure awk:

与纯awk

awk '{a[
sort test.file | uniq -cd | awk -v limit=2 ' > limit{print }'
]++}END{for(i in a){if(a[i] > 2){print i}}}' a.txt

It iterates over the file and counts the occurances of every IP. At the end of the file it outputs every IP which occurs more than 2 times.

它遍历文件并计算每个 IP 的出现次数。在文件的末尾,它输出出现 2 次以上的每个 IP。

回答by iruvar

Feed the output from uniq -cdto awk

将输出从uniq -cdawk

##代码##