【问题标题】:Linux command or script counting duplicated lines in a text file?Linux命令或脚本计算文本文件中的重复行?
【发布时间】:2011-09-20 19:22:00
【问题描述】:

如果我有一个包含以下内容的文本文件

red apple
green apple
green apple
orange
orange
orange

是否有我可以用来获得以下结果的 Linux 命令或脚本?

1 red apple
2 green apple
3 orange

【问题讨论】:

    标签: linux text duplicates


    【解决方案1】:

    你能接受一个按字母顺序排列的列表吗:

    echo "red apple
    > green apple
    > green apple
    > orange
    > orange
    > orange
    > " | sort -u 
    

    ?

    green apple
    orange
    red apple
    

    或

    sort -u FILE
    

    -u 代表唯一性,唯一性只能通过排序达到。

    保留顺序的解决方案:

    echo "red apple
    green apple
    green apple
    orange
    orange
    orange
    " | { old=""; while read line ; do   if [[ $line != $old ]]; then  echo $line;   old=$line; fi ; done }
    red apple
    green apple
    orange
    

    还有一个文件

    cat file | { 
    old=""
    while read line
    do
      if [[ $line != $old ]]
      then
        echo $line
        old=$line
      fi
    done }
    

    最后两个仅删除重复项,紧随其后 - 这适合您的示例。

    echo "red apple
    green apple
    lila banana
    green apple
    " ...
    

    将打印两个苹果,用一根香蕉分开。

    【讨论】:

      【解决方案2】:

      uniq -c file

      如果文件尚未排序:

      sort file | uniq -c

      【讨论】:

        【解决方案3】:
        cat <filename> | sort | uniq -c
        

        【讨论】:

          【解决方案4】:

          试试这个

          cat myfile.txt| sort| uniq
          

          【讨论】:

          • 没有 -c 或 -d 标志,uniq 无法区分重复行和非重复行,还是我遗漏了什么?
          【解决方案5】:

          通过sort 发送它(将相邻的项目放在一起)然后uniq -c 进行计数,即:

          sort filename | uniq -c
          

          并且要按排序顺序(按频率)获取该列表,您可以

          sort filename | uniq -c | sort -nr
          

          【讨论】:

          • 我用它来计算已安装的 RPM 包和许可证:$ rpm -qa --qf "%{license}\n" | sort | uniq -c | sort -nr &gt; ~/license_counts。更多信息here。谢谢。
          【解决方案6】:

          只需要计数:

          $> egrep -o '\w+' fruits.txt | sort | uniq -c
          
                3 apple
                2 green
                1 oragen
                2 orange
                1 red
          

          要获得排序计数:

          $> egrep -o '\w+' fruits.txt | sort | uniq -c | sort -nk1
                1 oragen
                1 red
                2 green
                2 orange
                3 apple
          

          编辑

          啊哈,这不是单词边界,我的错。这是用于整行的命令:

          $> cat fruits.txt | sort | uniq -c | sort -nk1
                1 oragen
                1 red apple
                2 green apple
                2 orange
          

          【讨论】:

            【解决方案7】:

            几乎与 borribles' 相同,但如果将 d 参数添加到 uniq,它只会显示重复项。

            sort filename | uniq -cd | sort -nr
            

            【讨论】:

            • 为-d 小便条竖起大拇指。
            【解决方案8】:

            这是一个使用Counter 类型的简单python 脚本。好处是这不需要对文件进行排序,基本上使用零内存:

            import collections
            import fileinput
            import json
            
            print(json.dumps(collections.Counter(map(str.strip, fileinput.input())), indent=2))
            

            输出:

            $ cat filename | python3 script.py
            {
              "red apple": 1,
              "green apple": 2,
              "orange": 3
            }
            

            或者您可以使用简单的单线:

            $ cat filename | python3 -c 'print(__import__("json").dumps(__import__("collections").Counter(map(str.strip, __import__("fileinput").input())), indent=2))'
            

            【讨论】:

              猜你喜欢
              • 2014-11-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-21
              • 1970-01-01
              • 2012-04-13
              • 1970-01-01
              相关资源
              最近更新 更多