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:
Options:
Option | Description |
---|---|
-c | Precede lines by the number of occurrences |
-d | Only print duplicate lines |
-u | Only print unique lines |
-f N | Skip the first N fields before checking for uniqueness |
-i | Ignore differences in case when comparing lines |
-s N | Skip the first N characters of each line before checking for uniqueness |
Parameters:
Parameter | Description |
---|---|
input_file | The file to check for duplicate or unique lines |
uniq bash Examples:
Remove Consecutive Duplicate Lines from a File
Removes consecutive duplicate lines from a file and displays the unique lines.
Keep Only One Instance of Consecutive Duplicate Lines
Displays only the duplicate lines that are repeated consecutively in the file.
Count the Number of Occurrences of Each Line
Sorts the file beforehand and counts the number of occurrences of each unique line.
Ignore Case Sensitivity when Comparing Lines
Performs the comparison of lines without considering the case sensitivity.
Display Only Unique Lines and Number of Occurrences
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
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:
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:
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:
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:
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:
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:
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:
Applications of the uniq command
- Remove duplicate lines from a file
- Count and display the number of occurrences of each line in a file
- Filter out adjacent matching lines
- Display only lines that are unique within a sorted file