3

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?

cuonglm
  • 150,973
  • 38
  • 327
  • 406

3 Answers3

10

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
cuonglm
  • 150,973
  • 38
  • 327
  • 406
  • Why are you using `-l` ? Also the `for` after the print is not needed if you remove the `-l`, because you aren't pointlessly stripping newlines. – 123 Feb 09 '16 at 15:09
  • 1
    Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/35503/discussion-between-cuonglm-and-123). – cuonglm Feb 09 '16 at 15:47
1

Try this AWK!

awk '{a[$0]++} END {for (x in a) if (a[x] == 1) print x}'
Firefly
  • 286
  • 3
  • 10
1
awk '{arr[$1]++} END {for (i in arr) {if (arr[i]==1) {print i} }}' 1
grapes
lime
peach
jai_s
  • 1,480
  • 7
  • 7