How to print only the unique lines in BASH?

后端 未结 3 1140
北恋
北恋 2020-12-17 08:47

How can I print only those lines that appear exactly once in a file? E.g., given this file:

mountain
forest
mountain
eagle

The output would

相关标签:
3条回答
  • 2020-12-17 09:14

    Use sort and uniq:

    sort inputfile | uniq -u
    

    The -u option would cause uniq to print only unique lines. Quoting from man uniq:

       -u, --unique
              only print unique lines
    

    For your input, it'd produce:

    eagle
    forest
    

    Obs: Remember to sort before uniq -u because uniq operates on adjacent lines. So what uniq -u actually does is to print lines that don't have identical neighbor lines, but that doesn't mean they are really unique. When you sort, all the identical lines get grouped together and only the lines that are really unique in the file will remain after uniq -u.

    0 讨论(0)
  • 2020-12-17 09:16

    You almost had the answer in your question:

    sort filename | uniq -u

    0 讨论(0)
  • 2020-12-17 09:37

    Using awk:

    awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' file
    eagle
    forest
    
    0 讨论(0)
提交回复
热议问题