2

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?

8

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
  • 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 9 '16 at 15:09
  • 1
    Let us continue this discussion in chat. – cuonglm Feb 9 '16 at 15:47
1
awk '{arr[$1]++} END {for (i in arr) {if (arr[i]==1) {print i} }}' 1
grapes
lime
peach
1

Try this AWK!

awk '{a[$0]++} END {for (x in a) if (a[x] == 1) print x}'
  • 1
    No, that still leaves the duplicate words, I want to print the only uniq words contained in the file. – MIlle82 Feb 9 '16 at 14:50
  • @MIlle82 UPS!! I hadn't understood your question!! – Firefly Feb 9 '16 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.