Skip to content

uniq Linux command

The uniq command in Linux is used to filter out duplicate lines from a file. It is often used in conjunction with other commands in a pipeline to streamline data and improve readability. By default, uniq compares adjacent lines in a file and removes any duplicates it finds. It also has options to ignore case, show only duplicates, or display the count of duplicate lines. uniq is a versatile tool for data manipulation and organization in the command line.

uniq Syntax:

Terminal window
uniq [option] [input_file]

Options:

OptionDescription
-cPrecede lines by the number of occurrences
-dOnly print duplicate lines
-uOnly print unique lines
-f NSkip the first N fields before checking for uniqueness
-iIgnore differences in case when comparing lines
-s NSkip the first N characters of each line before checking for uniqueness

Parameters:

ParameterDescription
input_fileThe file to check for duplicate or unique lines

uniq bash Examples:

Remove Consecutive Duplicate Lines from a File

Terminal window
uniq file.txt

Removes consecutive duplicate lines from a file and displays the unique lines.

Keep Only One Instance of Consecutive Duplicate Lines

Terminal window
uniq -d file.txt

Displays only the duplicate lines that are repeated consecutively in the file.

Count the Number of Occurrences of Each Line

Terminal window
sort file.txt | uniq -c

Sorts the file beforehand and counts the number of occurrences of each unique line.

Ignore Case Sensitivity when Comparing Lines

Terminal window
uniq -i file.txt

Performs the comparison of lines without considering the case sensitivity.

Display Only Unique Lines and Number of Occurrences

Terminal window
sort file.txt | uniq -c -u

Sorts the file first, displays only the unique lines, and includes the number of occurrences for each unique line.

Suppress Repeated Lines Based on a Specific Number of Characters

Terminal window
uniq -w 10 file.txt

Suppresses (or removes) repeated lines based on the first 10 characters of each line.

How do I use uniq in Linux?

To use the uniq command in Linux, execute the following command:

Terminal window
uniq file.txt

What is the purpose of uniq in Linux?

The uniq command in Linux is used to filter out adjacent duplicate lines in a file.

How can I count the number of occurrences of each line with uniq?

To count the number of occurrences of each line in a sorted file, use the -c option with uniq:

Terminal window
uniq -c sorted_file.txt

How can I display only the duplicated lines with uniq?

You can display only the duplicated lines in a sorted file by using the -d option with uniq:

Terminal window
uniq -d sorted_file.txt

How can I ignore the case sensitivity of lines with uniq?

To ignore the case sensitivity of lines while using uniq, you can use the -i option:

Terminal window
uniq -i file.txt

How can I display only the unique lines with uniq?

You can display only the unique lines in a sorted file by using the -u option with uniq:

Terminal window
uniq -u sorted_file.txt

How to skip a specific number of initial fields while comparing the lines?

To skip a specific number of initial fields while comparing the lines, use the -f option followed by the number of fields to skip with uniq:

Terminal window
uniq -f 2 file.txt

How can I display only the non-unique lines with uniq?

You can display only the non-unique lines in a sorted file by using the -D option with uniq:

Terminal window
uniq -D sorted_file.txt

Applications of the uniq command

  1. Remove duplicate lines from a file
  2. Count and display the number of occurrences of each line in a file
  3. Filter out adjacent matching lines
  4. Display only lines that are unique within a sorted file