The uniq
command won't work unless input is buffered (e.g. using sort
) or duplicated lines are next to each other. So find below few solutions.
AWK
Here is syntax for awk
:
command | awk '!a[$0]++{print}'
For repeated use, you can define the following shell alias:
alias unicat='awk "!a[\$0]++{print}"'
then run: command | unicat
.
Parallel syntax (for quicker parsing):
command | time parallel --block 100M --pipe awk '!a[\$0]++{print}'
Test command (100 lines filtered out of 200):
echo | pee "seq 1 100" "seq 1 100" | awk '!a[$0]++{print}' | wc -l
Perl
Try the following Perl script:
command | perl -e 'while(<>){if(!$s{$_}){print $_;$|=1;$s{$_}=1;}}'
Shell alias syntax for repeated use:
alias unicat='perl -e '\''while(<>){if(!$s{$_}){print $_;$|=1;$s{$_}=1;}}'\'''
then run: command | unicat
.
unique
For alternative to uniq
, you can also use karrick's unique
written in Go (it's using similar approach as above, but it's much faster and uses less memory than Perl/AWK solutions):
Usage:
command | unique
Cons: When processing huge data, it's using a lot of memory, as it's hashing each item and adding it into array.
Workaround can be to have short lived instances per certain block size (if few duplicates are acceptable), e.g.
command | parallel --block 100M -j 1 --pipe unique
or using split
(10M per chunk), e.g.
command | split -b 10M --filter="unique"
quniq
Use quniq
utility to remove duplicated. It works better than uniq
, as it buffers the input lines.
Cons: It seems to be stuck till end of the pipe.
huniq
If you're looking for faster solution to sort then remove duplicates, use huniq
.
More similar projects: