The uniq command

The following are a few examples showing the usage of the uniq command:

  • This command removes duplicate adjacent lines from the file:
    $ cat test
    aa
    aa
    cc
    cc
    bb
    bb
    yy
    zz
    $ uniq test
  • This output removes the duplicate adjacent lines from test file, shown as follows:
    aa
    cc
    bb
    yy
    zz
  • The next command only prints duplicate lines:
    $ uniq -d test
  • Output:
    aa
    cc
    bb
  • The following command prints the number of occurrences of all elements on an individual line:
    $ uniq -c test
  • Output:
    2 aa
    2 cc
    2 bb
    1 yy
    1 zz

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.