uniq

>Usage

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:

Dots

-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:

Dots

-zsh

> echo -e "apple\nbanana\napple" | uniq -d


apple

-u

-u (only unique lines): Shows only the lines that appear once.

Example 3:

Dots

-zsh

> echo -e "apple\nbanana\napple" | uniq -u


banana

-i

-i (ignore case): Ignores case when comparing lines.

Example 4:

Dots

-zsh

> echo -e "apple\nApple\nbanana" | uniq -i


apple
banana

-f

-f (skip fields): Skips the first N fields when comparing lines.

Example 5:

Dots

-zsh

> echo -e "apple red\napple green\nbanana yellow" | uniq -f 1


apple red
apple green
banana yellow

-s

-s (skip characters): Skips the first N characters when comparing lines.

Example 6:

Dots

-zsh

> echo -e "apple red\napple green\nbanana yellow" | uniq -s 6


apple red
apple green
banana yellow

-w

-w (compare N characters): Compares only the first N characters of each line.

Example 7:

Dots

-zsh

> echo -e "apple red\napple green\nbanana yellow" | uniq -w 5


apple red
banana yellow

-z

-z (zero-terminated): Treats input as zero-terminated.

Example 8:

Dots

-zsh

> echo -e "apple\0apple\0banana" | uniq -z


apple
banana

-n

-n (sort by fields): Sorts by the N-th field.

Example 9:

Dots

-zsh

> echo -e "apple red\nbanana yellow" | uniq -n


apple red
banana yellow

-v

-v (invert match): Shows only non-unique lines.

Example 10:

Dots

-zsh

> echo -e "apple\napple\nbanana" | uniq -v


banana

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Luca Fusetti