↑ ↓ select, Enter open, Esc close

Count the most frequent lines in a file

Adjust the values, the command updates live
user@server
sort list.txt | uniq -c | sort -rn | head -n 20

Sorts the lines so that identical entries are adjacent, lets uniq -c count the repetitions and sorts the result numerically in descending order. This is the basic pattern behind many analyses, for example of IP lists, error messages or usernames. Any command that produces a list fits in front of the first sort, such as cut or grep -o.

Note: uniq only detects directly consecutive duplicates, which is why the first sort is mandatory.

Also searched as

  • count occurrences of each line linux
  • sort uniq count frequency
  • top entries in a list command line

Related one-liners

All in Text processing

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.