Uniq

Table of Contents

uniq

Output the unique lines from the given input or file. Since it does not detect repeated lines unless they are adjacent, we need to sort them first. While piping sort into uniq is seen as the correct way to do things, piping uniq into sort is usually seen as incorrect.

  • Display each line once: sort .bashrc | uniq NOTE: fi only appears once even though it appears many times in the file. Also, be aware that simply using “sort -u” would achieve the same thing.
  • Display only unique lines: sort .bashrc | uniq -u NOTE: fi no longer is listed in the results. Only “unique” lines are returned.
  • Display only duplicate lines: sort .bashrc | uniq -d NOTE: The opposite of the above command.
  • Display number of occurrences of each line along with that line: sort .bashrc | uniq -c
  • Display number of occurrences of each line, sorted by the most frequent: sort .bashrc | uniq -c | sort -nr

Footer

Copyright © 2020-2021 Derek Taylor (DistroTube)

This page is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).

The source code for distro.tube can be found on GitLab. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.

Author: dt

Created: 2022-02-20 Sun 10:16