Linux Command Reference ยท Text Processing
Linux sort command
Reference for the Linux sort command: alphabetical and numeric order, sorting by field, reverse, unique, human sizes, stable sorts and the locale trap.
sort reads lines of text, orders them, and writes them back out. By default it compares whole lines as text according to the current locale. With options you can compare numbers, sort by a particular column, ignore case, remove duplicates, or understand version numbers and file sizes.
It is the workhorse behind many pipelines: uniq needs sorted input, comm and join need it too, and "top ten" reports are a sort | head away. For big inputs, sort spills to temporary files and merges, so it can handle files larger than memory.
sort [OPTION]... [FILE]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
sort [-bdfghiMnrRuVz] [-t SEP] [-k KEYDEF]... [-o FILE] [FILE]... sort -c|-C [OPTION]... [FILE] sort -m [OPTION]... FILE...
| Option | Description |
|---|---|
-n |
Compare by leading numeric value (integers and decimals, not exponents). |
-g |
General numeric comparison; understands 1e3, NaN and inf. Slower than -n. |
-h |
Human-readable numbers such as 2K, 1G. |
-V |
Natural version sort (1.2 before 1.10). |
-M |
Month names (JAN before FEB). |
-r |
Reverse the result. |
-f |
Fold lower case to upper case when comparing. |
-d |
Dictionary order: consider only blanks and alphanumerics. |
-b |
Ignore leading blanks in the key. |
-u |
Output only the first of each run of equal lines (equal as defined by the keys). |
-s |
Stable: disable the last-resort whole-line comparison. |
-t SEP |
Use SEP as the field separator instead of the blank-to-nonblank transition. |
-k START[,END] |
Sort by a key from field START to field END; modifier letters may follow each number, as in -k2,2nr. |
-o FILE |
Write to FILE; safe even if FILE is also an input. |
-c, -C |
Only check whether the input is sorted; -c complains, -C stays silent. |
-m |
Merge files that are already sorted. |
-R |
Random order (groups equal keys together). |
-z |
Lines end in NUL instead of newline. |
-T DIR, -S SIZE, --parallel=N |
Temp directory, memory buffer and thread count for big sorts. |
Returns: 0 on success. With -c or -C, 1 if the input is not sorted. 2 for other errors.
Usage notes
- The locale decides text order. In most UTF-8 locales
sortignores case and punctuation at first and the result differs between machines. UseLC_ALL=C sortfor byte order that is the same everywhere, and for speed. - Numbers sort as text without
-n:10comes before9. This is the most common mistake. - Keys:
-k2means "from field 2 to the end of the line". You nearly always want-k2,2to limit the key to one field. Modifiers after a number apply only to that key; placed before any-kthey apply globally. - Fields are counted from 1. Without
-t, fields are separated by runs of blanks and the blanks belong to the next field, so add-bwhen columns are aligned with spaces. - Ties are broken by the whole line, which makes the result deterministic but can surprise you. Add
-sto keep input order for equal keys. -uwith keys keeps only the first line for each distinct key, sosort -u -k1,1deduplicates by first column.sort -o file fileis the correct way to sort a file in place;sort file > filetruncates the file before reading it.- Large data: raise
-S(for example-S 50%) and point-Tat a fast disk with space. sort -Ris not a shuffle of equal lines; useshuffor real shuffling.- Check before relying on order:
sort -c filetells you whether a file is sorted forcommorjoin.
Related
uniq,shuf,cut,comm,join- Lessons: sort, uniq and cut, Text pipelines