SORT(1) General Commands Manual SORT(1)
sort - sort or merge files
sort [ -_________x ] [ +pos1 [ -pos2 ] ] ... [ -o name ] [ -T directory ] [ name ] ...
Sort sorts lines of all the named files together and writes the result on the standard output. The name `-' means the standard input. If
no input files are named, the standard input is sorted.
The default sort key is an entire line. Default ordering is lexicographic by bytes in machine collating sequence. The ordering is
affected globally by the following options, one or more of which may appear.
b Ignore leading blanks (spaces and tabs) in field comparisons.
d `Dictionary' order: only letters, digits and blanks are significant in comparisons.
f Fold upper case letters onto lower case.
i Ignore characters outside the ASCII range 040-0176 in nonnumeric comparisons.
n An initial numeric string, consisting of optional blanks, optional minus sign, and zero or more digits with optional decimal point, is
sorted by arithmetic value. Option n implies option b.
r Reverse the sense of comparisons.
tx `Tab character' separating fields is x.
The notation +pos1 -pos2 restricts a sort key to a field beginning at pos1 and ending just before pos2. Pos1 and pos2 each have the form
m.n, optionally followed by one or more of the flags bdfinr, where m tells a number of fields to skip from the beginning of the line and n
tells a number of characters to skip further. If any flags are present they override all the global ordering options for this key. If the
b option is in effect n is counted from the first nonblank in the field; b is attached independently to pos2. A missing .n means .0; a
missing -pos2 means the end of the line. Under the -tx option, fields are strings separated by x; otherwise fields are nonempty nonblank
strings separated by blanks.
When there are multiple sort keys, later keys are compared only after all earlier keys compare equal. Lines that otherwise compare equal
are ordered with all bytes significant.
These option arguments are also understood:
c Check that the input file is sorted according to the ordering rules; give no output unless the file is out of sort.
m Merge only, the input files are already sorted.
o The next argument is the name of an output file to use instead of the standard output. This file may be the same as one of the
T The next argument is the name of a directory in which temporary files should be made.
u Suppress all but one in each set of equal lines. Ignored bytes and bytes outside keys do not participate in this comparison.
Print in alphabetical order all the unique spellings in a list of words. Capitalized words differ from uncapitalized.
sort -u +0f +0 list
Print the password file (passwd(5)) sorted by user id number (the 3rd colon-separated field).
sort -t: +2n /etc/passwd
Print the first instance of each month in an already sorted file of (month day) entries. The options -um with just one input file make the
choice of a unique representative from a set of equal lines predictable.
sort -um +0 -1 dates
/usr/tmp/stm*, /tmp/* first and second tries for temporary files
uniq(1), comm(1), rev(1), join(1)
Comments and exits with nonzero status for various trouble conditions and for disorder discovered under option -c.
Very long lines are silently truncated.
7th Edition April 29, 1985 SORT(1)
Check Out this Related Man Page
SORT(1) BSD General Commands Manual SORT(1)
sort -- sort or merge text files
sort [-bcdfHilmnrSsu] [-k field1[,field2]] [-o output] [-R char] [-T dir] [-t char] [file ...]
The sort utility sorts text files by lines. Comparisons are based on one or more sort keys extracted from each line of input, and are per-
formed lexicographically. By default, if keys are not given, sort regards each input line as a single field.
The following options are available:
-c Check that the single input file is sorted. If the file is not sorted, sort produces the appropriate error messages and exits
with code 1; otherwise, sort returns 0. sort -c produces no output.
-H Ignored for compatibility with earlier versions of sort.
-m Merge only; the input files are assumed to be pre-sorted.
-o output The argument given is the name of an output file to be used instead of the standard output. This file can be the same as one of
the input files.
-S Don't use stable sort. Default is to use stable sort.
-s Use stable sort, keeps records with equal keys in their original order. This is the default. Provided for compatibility with
other sort implementations only.
-T dir Use dir as the directory for temporary files. The default is the value specified in the environment variable TMPDIR or /tmp if
TMPDIR is not defined.
-u Unique: suppress all but one in each set of lines having equal keys. If used with the -c option, check that there are no lines
with duplicate keys.
The following options override the default ordering rules. When ordering options appear independent of key field specifications, the
requested field ordering rules are applied globally to all sort keys. When attached to a specific key (see -k), the ordering options over-
ride all global ordering options for that key.
-d Only blank space and alphanumeric characters are used in making comparisons.
-f Considers all lowercase characters that have uppercase equivalents to be the same for purposes of comparison.
-i Ignore all non-printable characters.
-l Sort by the string length of the field, not by the field itself.
-n An initial numeric string, consisting of optional blank space, optional minus sign, and zero or more digits (including decimal
point) is sorted by arithmetic value. (The -n option no longer implies the -b option.)
-r Reverse the sense of comparisons.
The treatment of field separators can be altered using these options:
-b Ignores leading blank space when determining the start and end of a restricted sort key. A -b option specified before the first
-k option applies globally to all -k options. Otherwise, the -b option can be attached independently to each field argument of
the -k option (see below). Note that the -b option has no effect unless key fields are specified.
-t char char is used as the field separator character. The initial char is not considered to be part of a field when determining key
offsets (see below). Each occurrence of char is significant (for example, ``charchar'' delimits an empty field). If -t is not
specified, the default field separator is a sequence of blank-space characters, and consecutive blank spaces do not delimit an
empty field; further, the initial blank space is considered part of a field when determining key offsets.
-R char char is used as the record separator character. This should be used with discretion; -R <alphanumeric> usually produces undesir-
able results. The default record separator is newline.
Designates the starting position, field1, and optional ending position, field2, of a key field. The -k option replaces the obso-
lescent options +pos1 and -pos2.
The following operands are available:
file The pathname of a file to be sorted, merged, or checked. If no file operands are specified, or if a file operand is -, the
standard input is used.
A field is defined as a minimal sequence of characters followed by a field separator or a newline character. By default, the first blank
space of a sequence of blank spaces acts as the field separator. All blank spaces in a sequence of blank spaces are considered as part of
the next field; for example, all blank spaces at the beginning of a line are considered to be part of the first field.
Fields are specified by the -k field1[,field2] argument. A missing field2 argument defaults to the end of a line.
The arguments field1 and field2 have the form m.n and can be followed by one or more of the letters b, d, f, i, l, n, and r, which correspond
to the options discussed above. A field1 position specified by m.n (m, n > 0) is interpreted as the nth character in the mth field. A miss-
ing .n in field1 means '.1', indicating the first character of the mth field; if the -b option is in effect, n is counted from the first non-
blank character in the mth field; m.1b refers to the first non-blank character in the mth field.
A field2 position specified by m.n is interpreted as the nth character (including separators) of the mth field. A missing .n indicates the
last character of the mth field; m = 0 designates the end of a line. Thus the option -k v.x,w.y is synonymous with the obsolescent option
+v-1.x-1-w-1.y; when y is omitted, -k v.x,w is synonymous with +v-1.x-1-w+1.0. The obsolescent +pos1 -pos2 option is still supported, except
for -w.0b, which has no -k equivalent.
If the following environment variable exists, it is used by sort.
TMPDIR sort uses the contents of the TMPDIR environment variable as the path in which to store temporary files.
/tmp/sort.* Default temporary files.
outputNUMBER Temporary file which is used for output if output already exists. Once sorting is finished, this file replaces output
(via link(2) and unlink(2)).
Sort exits with one of the following values:
0 Normal behavior.
1 On disorder (or non-uniqueness) with the -c option
2 An error occurred.
comm(1), join(1), uniq(1), qsort(3), radixsort(3)
A sort command appeared in Version 5 AT&T UNIX. This sort implementation appeared in 4.4BSD and is used since NetBSD 1.6.
Posix requires the locale's thousands separator be ignored in numbers. It may be faster to sort very large files in pieces and then explic-
itly merge them.
This sort has no limits on input line length (other than imposed by available memory) or any restrictions on bytes allowed within lines.
To protect data sort -o calls link(2) and unlink(2), and thus fails on protected directories.
Input files should be text files. If file doesn't end with record separator (which is typically newline), the sort utility silently supplies
The current sort uses lexicographic radix sorting, which requires that sort keys be kept in memory (as opposed to previous versions which
used quick and merge sorts and did not.) Thus performance depends highly on efficient choice of sort keys, and the -b option and the field2
argument of the -k option should be used whenever possible. Similarly, sort -k1f is equivalent to sort -f and may take twice as long.
December 18, 2010 BSD