uniq Command in Linux

Learn via video courses
Topics Covered

Overview

The uniq command in Linux is a handy utility used for filtering out duplicate adjacent lines from text files or standard input, while preserving their order. It is commonly used for identifying unique occurrences, counting duplicates, and comparing files.

Linux uniq Command Syntax

The syntax for the uniq command is as follows:

Where:

  • OPTION: The flags and options that modify the behavior of the uniq command.
  • INPUT: The input file to be processed. If not specified, the standard input (stdin) is used.
  • OUTPUT: The output file where the results will be written. If not specified, the output is displayed on the standard output (stdout).

uniq Command Options:

  • -c, --count: Prefix lines by the number of occurrences.
  • -d, --repeated: Only print duplicate lines.
  • -u, --unique: Only print unique lines.
  • -i, --ignore-case: Ignore differences in case when comparing lines.

Example Usages

  • Remove duplicate adjacent lines from a text file.:

    Explanation: This command will remove duplicate adjacent lines from the 'input.txt' file and save the result in 'output.txt'.

  • Count the occurrences of each line in a file.:

    Output:

    Explanation: This command will count the occurrences of each line in the 'input.txt' file and display the result on the standard output.

Tips

  • Sort the input file before using the uniq command to get accurate results for non-adjacent duplicate lines.

Advanced Use Cases of uniq Command in Linux

  • Print only duplicate lines from a file.:

    Output:

    Explanation: This command will print only the duplicate lines from the 'input.txt' file on the standard output.

  • Print only unique lines from a file.:

    Output:

    Explanation: This command will print only the unique lines from the 'input.txt' file on the standard output.

  • Ignore case while comparing lines.:

    Output:

    Explanation: This command will ignore the case while comparing lines from the 'input.txt' file and display the result on the standard output.

Conclusion

  • The uniq command is used for filtering out duplicate adjacent lines.

  • It can count occurrences, print unique lines, or print duplicate lines.

  • The input can be a file or the standard input, and the output can be a file or the standard output.