Chapter 5. Redirection, Pipe and Shell Script

Uniq (Extract Unique Data Lines)

Uniq (Extract Unique Data Lines)
Tag:

The uniq (UNIQue) command is used to delete adjacent duplicated lines. With the sort command, you can extract unique data lines.

uniq command syntax

To explain the cut command, we use the purchase_product.txt file created in the previous section.

Command Line - INPUT
cat purchase_product.txt
Command Line - RESPONSE
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Grapes
Bananas
Bananas
Apple
Grapes

To delete adjacent duplicated lines, run the command below.

Command Line - INPUT
uniq purchase_product.txt

The result will be like the one below.

Command Line - RESPONSE
Apple
Orange
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Bananas
Apple
Grapes

uniq command with sort

To extract unique data lines, connect to the sort command using a pipe like shown below

Command Line - INPUT
sort purchase_product.txt | uniq
Command Line - RESPONSE
Apple
Bananas
Grapes
Orange

-c option

If you want to show numbers of duplicated lines, use the -c option.

Command Line - INPUT
sort purchase_product.txt | uniq -c
Command Line - RESPONSE
      5 Apple
      4 Bananas
      4 Grapes
      2 Orange

The uniq (UNIQue) command is used to delete adjacent duplicated lines. With the sort command, you can extract unique data lines.

uniq command syntax

To explain the cut command, we use the purchase_product.txt file created in the previous section.

Command Line - INPUT
cat purchase_product.txt
Command Line - RESPONSE
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Grapes
Bananas
Bananas
Apple
Grapes

To delete adjacent duplicated lines, run the command below.

Command Line - INPUT
uniq purchase_product.txt

The result will be like the one below.

Command Line - RESPONSE
Apple
Orange
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Bananas
Apple
Grapes

uniq command with sort

To extract unique data lines, connect to the sort command using a pipe like shown below

Command Line - INPUT
sort purchase_product.txt | uniq
Command Line - RESPONSE
Apple
Bananas
Grapes
Orange

-c option

If you want to show numbers of duplicated lines, use the -c option.

Command Line - INPUT
sort purchase_product.txt | uniq -c
Command Line - RESPONSE
      5 Apple
      4 Bananas
      4 Grapes
      2 Orange
Tag: