sort: sort lines of text
sort puts lines in order: alphabetically, by number, by size or by a particular column. It is usually the middle of a pipeline that counts or ranks something.
Common options
| Option | Meaning |
|---|---|
-n | Numeric order (2 before 10) |
-h | Human-readable sizes: 512K, 3M, 1.2G (GNU, BSD) |
-r | Reverse |
-k N / -k N,N | Sort by field N (to the end of the line / only field N) |
-t C | Fields are separated by character C |
-u | Drop duplicate lines |
-f | Ignore case |
-V | Version numbers: 1.9 before 1.10 (GNU, BSD) |
-o file | Write to a file (can be the input file) |
Examples
sort names.txt # alphabetical
sort -n numbers.txt # numeric
sort -rn counts.txt | head # largest first
du -sh * | sort -h # directory sizes, smallest to largest
sort -t: -k3,3n /etc/passwd # by user ID
sort -t, -k2,2 -k3,3nr data.csv # by column 2, then column 3 numeric descending
sort -u emails.txt # sorted, without duplicates
sort -V versions.txt # 2.9, 2.10, 2.11 in the right order
sort -o list.txt list.txt # sort a file in place
ps aux --sort=-%mem | head # ps can sort by itself (GNU ps)
Why does sort put lines in a strange order?
Sorting depends on the locale. In en_US.UTF-8, case and punctuation are partly ignored, so results differ from a simple byte order. For predictable results in scripts, especially before uniq or comm, set the C locale:
LC_ALL=C sort file.txt
It is also much faster on large files.
Large files
sort handles files larger than memory by using temporary files. Speed it up with more memory and threads (GNU): sort -S 2G --parallel=4 -T /var/tmp big.txt. -T chooses where temporary files go, useful when /tmp is small.
The classic counting pipeline
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head -10
Extract a column, sort it so equal values are together, count them with uniq -c, then sort the counts from highest to lowest.