uniq (Extract Unique Data Lines)
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.
cat purchase_product.txt
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Grapes
Bananas
Bananas
Apple
Grapes
To delete adjacent duplicated lines, run the command below.
uniq purchase_product.txt
The result will be like the one below.
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
sort purchase_product.txt | uniq
Apple
Bananas
Grapes
Orange
-c option
If you want to show numbers of duplicated lines, use the -c
option.
sort purchase_product.txt | uniq -c
5 Apple
4 Bananas
4 Grapes
2 Orange