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 uniq command can count and print the number of repeated lines. Just like duplicate lines, we can filter unique lines (non-duplicate lines) as well and can also ignore case sensitivity. We can skip fields and characters before comparing duplicate lines and also consider characters for filtering lines.
The uniq command in UNIX is a command line utility for reporting or filtering repeated lines in a file. It can remove duplicates, show a count of occurrences, show only repeated lines, ignore certain characters and compare on specific fields.
uniq command is used to detect the adjacent lines from a file and write the content of the file by filtering the duplicate values or write only the duplicate lines into another file.
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
.
Using awk:
awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' file
eagle
forest
You almost had the answer in your question:
sort filename | uniq -u
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With