OpenBSD manual page server

Manual Page Search Parameters

SORT(1) General Commands Manual SORT(1)

sortsort, merge, or sequence check text files

sort [-bCcdfHimnrsuz] [-k field1[,field2]] [-o output] [-R char] [-T dir] [-t char] [file ...]

The sort utility sorts text files by lines, operating in one of three modes: sort, merge, or check. In sort mode, the specified files are combined and sorted by line. Merge mode is the same as sort mode except that the input files are assumed to be pre-sorted. In check mode, a single input file is checked to ensure that it 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 options are as follows:

Check that the single input file is sorted. If it is, exit 0; if it's not, exit 1. In either case, produce no output.
Like -C, but additionally write a message to if the input file is not sorted.
Merge only; the input files are assumed to be pre-sorted. This option is overridden by the -C or -c options, if they are also present.
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.
dir
Use dir as the directory for temporary files. The default is the contents of the environment variable TMPDIR or /var/tmp if TMPDIR does not exist.
Unique: suppress all but one in each set of lines having equal keys. If used with the -C or -c options, also check that there are no lines with duplicate keys.

The following options override the default ordering rules globally:

Use a merge sort instead of a radix sort. This option should be used for files larger than 60MB.
Enable stable sort. Uses additional resources (see sradixsort(3)).

The following options override the default ordering rules. If ordering options appear before the first -k option, they apply globally to all sort keys. When attached to a specific key (see -k), the ordering options override all global ordering options for that key. Note that the ordering options intended to apply globally should not appear after -k or results may be unexpected.

Only blank space and alphanumeric characters are used in making comparisons.
Considers all lowercase characters that have uppercase equivalents to be the same for purposes of comparison.
Ignore all non-printable characters.
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.)
Reverse the sense of comparisons.

The treatment of field separators can be altered using these options:

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 -b should not appear after -k, and that it has no effect unless key fields are specified.
char
char is used as the record separator character. This should be used with discretion; -Ralphanumeric⟩ usually produces undesirable results. The default record separator is newline.
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. 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 delimit an empty field; further, the initial blank space considered part of a field when determining key offsets.
Uses the nul character as the record separator.

Sort keys are specified with:

-k field1[,field2]
Designates the starting position, field1, and optional ending position, field2, of a key field. The -k option may be specified multiple times, in which case subsequent keys are compared after earlier keys compare equal. The -k option replaces the obsolescent 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 maximal sequence of characters other than the field separator and record separator (newline by default). Initial blank spaces are included in the field unless -b has been specified; the first blank space of a sequence of blank spaces acts as the field separator and is included in the field (unless -t is specified). For example, by default 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, n, and r, which correspond to the options discussed above. A field1 position specified by m.n is interpreted as the nth character from the beginning of the mth field. A missing .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. 1.n refers to the nth character from the beginning of the line; if n is greater than the length of the line, the field is taken to be empty.

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 is omitted, -k v.x,w is synonymous with +v-1.x-1 -w.0. The obsolescent +pos1 -pos2 option is still supported, except for -w.0b, which has no -k equivalent.

Path in which to store temporary files. Note that TMPDIR may be overridden by the -T option.

/var/tmp/sort.*
default temporary directories
output#PID
temporary name for output if output already exists

The sort utility exits with one of the following values:

0
Normal behavior.
1
The input file is not sorted and -C or -c was given, or there are duplicate keys and -Cu or -cu was given.
2
An error occurred.

comm(1), join(1), uniq(1), radixsort(3)

The sort utility is compliant with the IEEE Std 1003.1-2008 (“POSIX.1”) specification.

The flags [-HRsTz] are extensions to that specification.

A sort command appeared in Version 3 AT&T UNIX.

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.

The current sort command 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.

To sort files larger than 60MB, use sort -H; files larger than 704MB must be sorted in smaller pieces, then merged.

August 24, 2013 OpenBSD-5.6