Print only unique lines from file not the duplicates

awkperlsedtext processing

I have a sorted words list, line by line file like so:

apple
apple
grapes
lime
orange
orange
pear
pear
peach
strawberry
strawberry

I only want to print out unique lines and drop duplicates:

grapes
peach
lime

How can I do this with sed, awk or perl?

Best Answer

That's the job for uniq:

$ LC_ALL=C uniq -u file
grapes
lime
peach

If you want other tools, like perl:

perl -nle '$h{$_}++; END {print for grep { $h{$_} == 1 } %h}' <file
Related Question