【问题标题】:How to print most frequent lines of file in linux terminal?如何在 linux 终端中打印最频繁的文件行?
【发布时间】:2014-11-11 16:43:31
【问题描述】:

我有带行的文件:

<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.26 192.168.0.22 192.168.0.23

【问题讨论】:

  • 方法有很多。尝试给出一个示例输入文件、所需的输出......当然还有你到目前为止所尝试的。
  • 超级用户不应该问这个吗?这个网站是写软件的。
  • here-output-your-ips | sort | uniq -c | sort -n -k1 | head -n 5

标签: linux file unix terminal


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 2018-10-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多