Sortix
Sortix Download Manual Development Source Code News Blog More
current nightly

Sortix nightly manual

This manual documents Sortix nightly, a development build that has not been officially released. You can instead view this document in the latest official manual.

SORT(1) General Commands Manual SORT(1)

NAME

sort — sort lines of text

SYNOPSIS

sort [-bCcdfgihMmnRruVz] [-k key] [-o path] [-t separator] file ...

DESCRIPTION

sort reads lines of text from the standard input and writes the lines in sorted order to the standard output. If files are specified, the input is the concatenated content of the files read in sequential order. The file path can be set to ‘-’ to specify the standard input. The lines are compared according to the current locale's collating rules.

The options are as follows:

-b, --ignore-leading-blanks
Ignore leading blank characters (space and tab) when determining fields.
-c, --check, --check=diagnose-first
Check whether the input is already sorted. If a line is out of order (or an equal line is found if -u), write an error describing which line was out of order and exit 1.
-C, --check=quiet, --check=silent
Same as -c, but write no error to the standard output about the input being out of order.
-d, --dictionary-order
Only alphanumeric and blank characters are significant when comparing.
-f, --ignore-case
Compare lowercase characters as if they were uppercase.
-g, --general-numeric-sort, --sort=general-numeric
Compare the strings as floating-pointer numbers parsed with strtod(3).
-i, --ignore-nonprinting
Ignore non-printable characters when comparing.
-k key, --key=key
Add another sorting key in order of decreasing priorities with the format:

starting_field[modifier][,ending_field[modifier]]

The lines are split into fields per the -t option. Each key compares lines starting from the start of the starting_field until the end of the ending_field. If every key compares equal, the lines are compared with strcoll(3) as a fallback. Optional modifier strings using characters from the set bdfghiMnRrV select the comparison behavior from the option by that name. If no keys are specified, a key is added spanning the whole line.

-h, --human-numeric-sort, --sort=human-numeric
Compare numeric values followed by a unit magnitude character from the YZEPTGMKk set.
-M, --month-sort, --sort=month
Compare month names.
-m, --merge
Merge the presorted input files into a sorted output.
-n, --numeric-sort, --sort=numeric
Compare numeric values.
-o path, --output=path
After reading the full input; write the output to the file at path (creating it if it does not already exist, discarding its previous contents if it already existed). The output file can be one of the input files. This option is incompatible with -C and -c.
-R, --random-sort, --sort=random
Sort the lines randomly with a uniform distribution, where all permutations are equally likely. This option is incompatible with -C and -c. If -u, don't write duplicate lines to the output.
-r, --reverse
Compare the lines in reverse order.
-t separator, --field-separator=separator
Use separator as the character that separates fields for use in -k. Each occurence of the character is significant. If -t is not specicied, fields are split on sequences of blank characters, where multiple occurences are not significant.
-u, --unique
Don't write a line if it is equal to the previous line.
-V, --version-sort, --sort=version
Sort according to the version string, per strverscmp(3).
-z, --zero-terminated
Lines are delimited with the NUL byte (0) instead of the newline byte (10).

IMPLEMENTATION NOTES

In the event of an input error, sort will write an error to the standard error and exit unsuccessfully.

sort reads the whole input into memory, rather than storing intermediate sorting steps in the filesystem, and requires enough memory to store a copy of the whole input.

ENVIRONMENT

LANG
The default locale for locale variables that are unset or null.
LC_ALL
Overrides all the other locale variables if set.
LC_COLLATE
Compare the input according to this locale's collating rules using strcoll(3).

EXIT STATUS

sort will exit 0 on success, exit 1 if the input was out of order when -C or -c, or exit 2 (or higher) otherwise.

EXAMPLES

Read lines from the standard input and write them in sorted order to the standard output:

sort < input > output

Read lines from the three specified files (where the second happens to be the standard input) and write them in sorted to the standard output:

grep pattern lines.txt | sort foo - bar -o output.txt

Sort the input file if it isn't already sorted:

if sort -C file; [ $? = 1 ]; then
  sort file -o file
fi

Remove duplicate lines from the input by sorting it and removing lines equal to the previous line:

sort -u

SEE ALSO

cat(1), comm(1), join(1), uniq(1), qsort(3), strcoll(3), strverscmp(3)

STANDARDS

sort is standardized in IEEE Std 1003.1-2008 (“POSIX.1”).

The -g, -h, -M, -R, -V, and -z options, as well as the long options, are extensions also found in GNU coreutils.

Unlike GNU coreutils, -R will not remove duplicates unless -u is passed.

As an extension, the -C and -c options support multiple input files.

BUGS

The -m option is not currently taken advantage of to speed up the sorting, rather the presorted input files are sorted all over again.

April 8, 2018 Sortix 1.1.0-dev
Copyright 2011-2025 Jonas 'Sortie' Termansen and contributors.
Sortix's source code is free software under the ISC license.
#sortix on irc.sortix.org
@sortix_org