uniq command in Linux and Unix-like operating systems is used to filter or display unique lines from a sorted file or input received from standard input. It eliminates consecutive duplicate lines and displays only the unique lines in the output.
The basic syntax of the
uniq command is as follows:
uniq [options] [file]
[options] represents the various flags and parameters you can use with the command, and
[file] refers to the file you want to process. If no file is specified,
uniq reads input from standard input.
Some commonly used options with
-c: Prefixes each line with the count of occurrences.
-d: Only displays duplicate lines.
-i: Performs a case-insensitive comparison.
-f: Skips a specified number of fields before comparing.
-s: Skips a specified number of characters before comparing.
To use the
uniq command, open a terminal and enter the command followed by the desired options and the file you want to process. Here are a few examples:
- Display only unique lines in a file:
- Display unique lines with the count of occurrences:
uniq -c filename
- Display only duplicate lines in a file:
uniq -d filename
- Perform a case-insensitive comparison to filter unique lines:
uniq -i filename
- Skip the first field before comparing lines for uniqueness:
uniq -f 1 filename
These are just a few examples, and there are many more options and variations you can use with the
uniq command. To explore additional options and information, you can refer to the
uniq command’s manual page by typing
man uniq in the terminal.