| SORT(1) | General Commands Manual | SORT(1) |
sort — sort, merge
or check the sortedness of text files
sort |
[-bdfHilnrSsu] [-k
kstart[,kend]
...] [-o
output] [-R
char] [-T
dir] [-t
char] [file ...] |
sort |
-m [-bdfHilnrSsu]
[-k
kstart[,kend]
...] [-o
output] [-R
char] [-T
dir] [-t
char] [file ...] |
sort |
-C|-c
[-bdfilnru] [-k
kstart[,kend]
...] [-R
char] [-t
char] [file] |
The sort utility performs one of three
operations on its input files:
-C,
-c or -m are specified,
the sort utility will sort the lines of the
specified input files together and write the result to the specified
output.-m option is specified, the
sort utility will merge the lines of the specified
input files and write the result to the specified output. The input files
are expected to be presorted for this mode of operation.-C or -c
options are specified, the sort utility will
indicate by its exit status whether a single input file is correctly
sorted.Comparisons are based on one or more sort keys extracted from each
line of input, and are performed lexicographically. By default, if keys are
not given, sort regards each input line as a single
field.
The following options are available:
-C-c without the error messages in the
case of unsorted input.-csort produces the appropriate error messages and
exits with code 1; otherwise, sort returns 0.
sort -c produces no
output. See also -u.-Hsort.-m-o
output-S-ssort implementations only.-T
dirTMPDIR or /tmp if
TMPDIR is not defined.-u-c option, check that there are no
lines with duplicate keys.The following options, which should be given before any
-k options, override the default ordering rules.
When ordering options appear independent of, and before, 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 override all global
ordering options for that key.
-d-f-i-l-n-n option no
longer implies the -b option).-rThe treatment of field separators can be altered using these options:
-b-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.-k
kstart[,kend]-k option replaces the obsolescent options
+pos1 and
-pos2.-R
char-R
⟨alphanumeric⟩ usually produces
undesirable results. If char is not a single character, then it specifies
the value of the desired record separator as an integer specified in any
of the normal ddd,
0ooo, or
0xhh ways, or as an octal
value preceded by a backslash (‘\’).
Caution:
do not attempt to specify ‘\^A’
(SOH) as ‘-R
1’ which will not do what was intended at
all! The default record separator is newline.-t
char-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.The following operands are available:
-, 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
kstart[,kend]
argument. A missing kend argument defaults to the end
of a line.
The arguments kstart and
kend have the form
m[.n]
(m, n >
0) 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 kstart position specified by
m.n is
interpreted as the n'th character in the
m'th field. A missing
.n in
kstart means
‘.1’, indicating the first character
of the m'th field; if the -b
option is in effect, n is counted from the first
non-blank character in the m'th field;
m.1b refers to the first
non-blank character in the m'th field.
A kend position specified by
m.n is
interpreted as the n'th character (including
separators) of the m'th field. A missing
.n indicates the last
character of the m'th field; m =
0 designates the end of a line.
Thus the option -k
v.x,w.y
is synonymous with the obsolescent options
+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 options are still
supported, except for
-w.0b,
which has no -k equivalent.
sort compares records by comparing the key
fields selected by -k arguments, from first given to
last, until discovering a difference. If there are no
-k arguments, the whole record is treated as a
single key. After exhausting the -k arguments, if no
difference has been found, then the result depends upon the
-u and -S option settings.
With -u the records are considered identical, and
one is suppressed. Otherwise with -s set (default)
the records are left in their original order, or with
-S (POSIX mode) the whole record is considered as a
tie breaker.
If the following environment variable exists, it is used by
sort.
TMPDIRsort uses the contents of the
TMPDIR environment variable as the path in which
to store temporary files.To silently check if the lines in a file are sorted lexicographically, use:
$ sort -C fileTo merge three pre-sorted files and write the merged result to stdout, use:
$ sort -m file1 file2
file3To sort lines lexicographically using the second whitespace-separated field of each line as the sort key preserving leading whitespace in the sort key, use:
$ sort -k 2 <<EOF aa bb ab bb EOF ab bb aa bb $
To sort lines lexicographically using the second whitespace-separated field on each line as the sort key ignoring any leading whitespace in the sort key, use:
$ sort -k 2b <<EOF aa bb ab bb EOF aa bb ab bb $
To sort lines numerically, using the second whitespace-separated field on each line as the sort key, starting the sort key at the second non-blank character within the field, use:
$ sort -k 2.2nb <<EOF aa X42 ab Y24 EOF ab Y24 aa X42 $
To sort lines numerically with a sort key starting on the second character of the second whitespace delimited field and to further sort lines with equal keys lexicographically on the first whitespace delimited field, use:
$ sort -k 2.2n -k 1 <<EOF ab X42 aa Y42 ac Z43 EOF aa Y42 ab X42 ac Z43
To lexicographically sort the lines of a file in reverse order, use:
$ sort -r -f fileSort exits with one of the following values:
-c (or
-C) option.A sort command appeared in
Version 1 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 explicitly 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 one.
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 kend
argument of the -k option should be used whenever
possible. Similarly, sort
-k1f is equivalent to sort
-f and may take twice as long.
| January 23, 2026 | NetBSD 11.99 |