uniq is a command used to filter out repeated lines in a text file. It’s perfect for cleaning up duplicate entries, leaving only the unique occurrences of each line in the file or stream.
>Synopsis
uniq [options] [input_file [output_file]]
>Options
-c
-c (count occurrences): Shows how many times each line appears.
Example 1:
-zsh
> echo -e "apple\napple\nbanana" | uniq -c
2 apple 1 banana
-d
-d (only repeated lines): Shows only the lines that appear more than once.
Example 2:
-zsh
> echo -e "apple\nbanana\napple" | uniq -d
apple
-u
-u (only unique lines): Shows only the lines that appear once.
Example 3:
-zsh
> echo -e "apple\nbanana\napple" | uniq -u
banana
-i
-i (ignore case): Ignores case when comparing lines.
Example 4:
-zsh
> echo -e "apple\nApple\nbanana" | uniq -i
apple banana
-f
-f (skip fields): Skips the first N fields when comparing lines.