How to print only the unique lines in BASH?

给你一囗甜甜゛ 提交于 2020-01-11 04:26:42

问题


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 be this, because the line mountain appears twice:

forest
eagle
  • The lines can be sorted, if necessary.

回答1:


Using awk:

awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' file
eagle
forest



回答2:


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.




回答3:


You almost had the answer in your question:

sort filename | uniq -u



来源:https://stackoverflow.com/questions/23740545/how-to-print-only-the-unique-lines-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!