Search notes:

Shell command: sort

sort sorts lines of text files.
sort is especially useful in conjunction with uniq (because uniq needs the input lines to be sorted).
However, the traditional sequence sort | uniq can nowadays usually be abbreviavated with sort -u

Options

Options controlling sorting

-b --ignore-leading-blanks ignore leading blanks
-d --dictionary-order consider only blanks and alphanumeric characters
-f --ignore-case fold lower case to upper case characters
-g --general-numeric-sort compare according to general numerical value
-i --ignore-nonprinting consider only printable characters
-M --month-sort compare (unknown) < 'JAN' < ... < 'DEC'
-h --human-numeric-sort compare human readable numbers (e.g., 2K 1G)
-n --numeric-sort compare according to string numerical value
-R --random-sort Shuffle, but group identical keys. See also shuf(1)
--random-source =FILE get random bytes from FILE
-r --reverse reverse the result of comparisons
--sort =WORD sort according to WORD. General-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V
-V --version-sort natural sort of (version) numbers within text. Compare ls -v

Other options

--batch-size =NMERGE merge at most NMERGE inputs at once; for more use temp files
-c --check =diagnose-first Check for sorted input; do not sort
-C --check=quiet Like -c, but do not report first bad line (compare --check=silent)
--compress-program =PROG compress temporaries with PROG; decompress them with PROG -d
--debug annotate the part of the line used to sort, and warn about questionable usage to stderr
--files0-from =F Read input from the files specified by NUL-terminated names in file F; If F is - then read names from stdin.
-k --key =KEYDEF Sort via a key; KEYDEF gives location and type
-m --merge merge already sorted files; do not sort
-o --output =FILE write result to FILE instead of stdout
-s --stable stabilize sort by disabling last-resort comparison
-S --buffer-size =SIZE use SIZE for main memory buffer
-t --field-separator =SEP use SEP instead of non-blank to blank transition
-T --temporary-directory =DIR use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories
--parallel =N change the number of sorts run concurrently to N
-u --unique with -c, check for strict ordering; without -c, output only the first of an equal run. Compare with uniq
-z --zero-terminated line delimiter is NUL, not newline
--help
--version

In place sorting

The -o flag specifies an output file name for the sorted file.
If the specified file name is equal to the sorted file, this file will then have the sorted content.
cat > to-be-sorted <<"EOF"
one
two
three
four
five
EOF

sort to-be-sorted -o to-be-sorted

cat to-be-sorted
Github repository shell-commands, path: /sort/o_in-place

-n: sort numerically

With -n, sort sorts values numerically rather than alphabetically.
Without this flag, sort considers 123 to be smaller than 89.

-h: sort human readable numbers

-h causes the sorted values to be interpreted as human readable, for example 1.89G or 815M.
Incidentally, ls also has an -h flag which reports file sizes human readable.
Thus the output of ls with the -h flag can be piped in sort with the -h flag to show a directory's largest files with file sizes human readable.

-k: keydef

-k allows to define a start and optional stop position:
-k startpos[,stoppos]
The format of startpos and stoppos is F[.C][OPTS]:
F Field number
C Character pos in the field
OPTS a series of single-letter ordering options

Locale

The sort order is influenced by the current locale in effect.
«Traditional» sort order (i. e. sorting by byte value) can be achieved by setting LC_ALL to C.

Strange results

Use the --debug option.

Environment variables

LC_CTYPE

See also

shuf randomly permutates the input lines.
Shell commands
R function: sort, Perl function: sort, vim script: sort

Index