Enhancing Skills

uniq: Report or Filter Out Repeated Lines in a File

Description:
The uniq command filters out or reports repeated lines in a file. It is often used in combination with the sort command to remove duplicates from sorted data.

Command:

uniq [options] [input_file] [output_file]

Sample Input

file.txt:

apple
banana
banana
orange
apple
grape
grape

Sample Output (Default):

apple
banana
orange
apple
grape

Options:

  • -c: Prefix lines with the number of occurrences.
  • -d: Only print duplicate lines.
  • -u: Only print unique lines (lines that are not repeated).
  • -i: Ignore case while comparing lines.

Example with -c Option:

To count occurrences of each line:

uniq -c file.txt

Sample Output with -c:

      1 apple
      2 banana
      1 orange
      1 apple
      2 grape

Example with -d Option:

To display only duplicate lines:

uniq -d file.txt

Sample Output with -d:

banana
grape

Example with -u Option:

To display only unique lines (lines that are not repeated):

uniq -u file.txt

Sample Output with -u:

orange

Example with -i Option:

To ignore case while comparing lines:

uniq -i file.txt

Sample Input file.txt:

Apple
banana
BANANA
orange
apple
GRAPE
grape

Sample Output with -i:

Apple
banana
orange
GRAPE

The uniq command is useful for processing text files to eliminate or analyze duplicate lines, especially when used in conjunction with sorting.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.