uniq command in Linux

uniq command is used to find only unique lines in a file.

  1. uniq <file-name> : This command will display only unique lines in the file. Note that only adjacent duplicate lines are removed. To remove all duplicates, you can sort the contents of file and then pipe the output to uniq command.
  2. uniq -c <file-name> : Here -c stands for count. It displays the count of occurences of each line.
  3. uniq -d <file-name>: Here -d stands for duplicate. It displays the duplicate lines once for each set of duplicate occurence.
  4. uniq -D <file-name> : It displays all occurrences of duplicate lines.
  5. uniq -u <file-name> : It displays only unique line.
  6. uniq -c -w 4 <file-name> :Use first 4 chars for checking uniqueness
  7. uniq -D -s 4 <file-name> : avoid comparing first 4 chars
  8. uniq -D -f 4 <file-name> : avoid compaing first 4 words

You may also like...