Linux uniq Command
The uniq
command in Linux is used to filter out or report repeated lines in a file or input. It is often used in conjunction with the sort
command, as uniq
only identifies duplicates that are adjacent. This means you typically want to sort the data first if you're looking to remove all duplicates from an unsorted file.
Basic Syntax
Common Options
-c
: Counts the number of occurrences of each line.-d
: Displays only duplicate lines.-u
: Displays only unique lines.-i
: Ignores case differences when comparing lines.
Examples with Output
Example 1: Basic Usage
Suppose you have a file named file.txt
that contains the following lines:
Command
Output
Explanation:
- Since the lines are not adjacent,
uniq
does not remove any duplicates in this case.
Example 2: Using uniq
After sort
To remove all duplicates regardless of their positions, first sort the file.
Command
Output
Explanation:
- The
sort
command organizes the lines, placing identical lines next to each other. Then,uniq
removes duplicates, leaving only one instance of each line.
Example 3: Counting Occurrences with -c
To count how many times each line appears, use the -c
option.
Command
Output
Explanation:
- Each line is preceded by a count of occurrences. In this case,
apple
andbanana
both appear twice, whilecherry
appears once.
Example 4: Displaying Only Duplicates with -d
To show only the lines that appear more than once, use the -d
option.
Command
Output
Explanation:
- This command lists only the lines that are duplicated, so only
apple
andbanana
are shown.
Example 5: Displaying Only Unique Lines with -u
To show only the lines that are unique (not repeated), use the -u
option.
Command
Output
Explanation:
- The output shows only lines that appear once, so
cherry
is the only unique line.
Example 6: Ignoring Case Differences with -i
To ignore case when determining duplicates, use the -i
option.
Suppose file2.txt
contains:
Command
Output
Explanation:
- The command ignores the case of
apple
andApple
, as well asbanana
andBANANA
, resulting in only one instance of each unique line being shown.
Summary of Common uniq
Commands
Command | Description |
---|---|
uniq file.txt | Filters out duplicate lines, keeping only unique ones. |
`sort file.txt | uniq` |
`sort file.txt | uniq -c` |
`sort file.txt | uniq -d` |
`sort file.txt | uniq -u` |
`sort file2.txt | uniq -i` |
The uniq
command is useful for processing text data, especially when working with sorted input to manage duplicates efficiently.