gpt4 book ai didi

linux - 如何在Linux终端中打印文件中最频繁的行?

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:09 25 4
gpt4 key购买 nike

我有包含以下行的文件:

<host>\t<ip>\n

我需要打印前 5 个最常见的 IP。我怎样才能做到这一点?

例如,如果我需要从此文件中打印 3 个最常见的 IP:

host1   192.168.0.26
host2 192.168.0.26
host3 192.168.0.23
host4 192.168.0.24
host5 192.168.0.26
host6 192.168.0.26
host7 192.168.0.25
host8 192.168.0.26
host9 192.168.0.26
host18 192.168.0.22
host22 192.168.0.22
host24 192.168.0.23

我会打印:192.168.0.26192.168.0.22192.168.0.23

最佳答案

以下内容应该有效。请注意,即使有 10 个具有相同频率的 IP,它也会返回 5 行。

cut -f2 file | sort | uniq -c | sort -n | head -n5

关于linux - 如何在Linux终端中打印文件中最频繁的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com