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]
Copy

001uniq [options] [input_file] [output_file]

Sample Input

file.txt:

apple
banana
banana
orange
apple
grape
grape
Copy

001apple

002banana

003banana

004orange

005apple

006grape

007grape

Sample Output (Default):

apple
banana
orange
apple
grape
Copy

001apple

002banana

003orange

004apple

005grape

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
Copy

001uniq -c file.txt

Sample Output with -c:

      1 apple
      2 banana
      1 orange
      1 apple
      2 grape
Copy

001 1 apple

002 2 banana

003 1 orange

004 1 apple

005 2 grape

Example with -d Option:

To display only duplicate lines:

uniq -d file.txt
Copy

001uniq -d file.txt

Sample Output with -d:

banana
grape
Copy

001banana

002grape

Example with -u Option:

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

uniq -u file.txt
Copy

001uniq -u file.txt

Sample Output with -u:

orange
Copy

001orange

Example with -i Option:

To ignore case while comparing lines:

uniq -i file.txt
Copy

001uniq -i file.txt

Sample Input file.txt:

Apple
banana
BANANA
orange
apple
GRAPE
grape
Copy

001Apple

002banana

003BANANA

004orange

005apple

006GRAPE

007grape

Sample Output with -i:

Apple
banana
orange
GRAPE
Copy

001Apple

002banana

003orange

004GRAPE

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.