uniq
requires the input to be sorted (from man uniq
) if you want it to remove all duplicate lines:
DESCRIPTION
Filter adjacent matching lines from INPUT (or standard input), writing
to OUTPUT (or standard output).
As you can see above, it only filters adjacent matching lines. This is why the lol
s were removed. So sort your data before passing to uniq
:
$ printf "lol\nlol\nfoo\n\n\n\n\nbar\nlol\nlol\nfoo\nlol\nfoo" | sort | uniq
bar
foo
lol
Or, with GNU sort
, skip uniq
:
$ printf "lol\nlol\nfoo\n\n\n\n\nbar\nlol\nlol\nfoo\nlol\nfoo" | sort --unique
bar
foo
lol
Finally, if you want to completely remove lines that were present more than once (instead of keeping one copy, the default behavior), use uniq -u
or --unique
as in your question:
$ printf "lol\nlol\nfoo\n\n\n\n\nbar\nlol\nlol\nfoo\nlol\nfoo" | sort | uniq -u
bar
In all cases, however, the sorting is necessary.