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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.