uniq

The uniq command finds the unique lines in a given input (stdin or a filename command line argument) and either reports or removes the duplicated lines.

This command only works with sorted data. Hence, uniq is often used with the sort command.

To produce the unique lines (all lines in the input are printed and duplicate lines are printed once), use this:

$ cat sorted.txt
bash 
foss 
hack 
hack

$ uniq sorted.txt
bash 
foss 
hack 

Alternatively, use this:

$ sort unsorted.txt | uniq

Display only unique lines (the lines that are not repeated or duplicated in the input file):

$ uniq -u sorted.txt
bash
foss

Alternatively, use this command:

$ sort unsorted.txt | uniq -u

To count how many times each of the lines appears in the file, ...

Get Linux Shell Scripting Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.