SORT(1)

NAME
sort - sort or merge text files

SYNOPSIS
sort [-mubdfinrtx] [ +pos1 [-pos2]] ... [-o output] [-T directory] [file] ...

DESCRIPTION
The sort utility sorts text files by lines. Comparisons are based on one or more sort keys (or fields) 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  Check that the single input file is sorted lexicographically. If the file is not sorted, sort sorts it and writes the sorted output to the standard output or the filename specified by the -o option.

-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.

-T directory
    The argument directory is used for creating temporary files.

-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.

-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.

-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 implies the -b option. (See below.) Note that the -b option is only effective when key fields have been specified and that -0 is considered equal to zero. Reverse the sense of comparisons.

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

-b  Leading blank spaces are ignored when determining the starting ending positions of a restricted sort key. If the -b option is specified before the first +pos1 argument, it shall be applied to all +pos1 arguments. Otherwise, the -b option can be attached independently to each +pos1 or - pos2 argument (see below).

-t char
    Char is used as the field separator character; char is not considered to be part of a field (although it can be included in a sort key). Each occurrence of char is significant (for example, “charchar” delimits an empty field). If -t is not specified, blank space characters are used as default field separators.

+pos1
    Designates the start position of a key field.

-pos1
    Designates the end position of a key field.

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 to be 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 +pos1 and - pos2 arguments. A missing +pos1 argument defaults to the beginning of a line. A missing - pos2 argument defaults to the end of a line.

The arguments +pos1 and - pos2 have the form m.n followed by one or more of the options -b, -d, -f, -i, -n, -r. A +pos1 position specified by m.n is interpreted to mean the nth character in the m+1th field. A missing.n means ‘.0’, indicating the first character of the m+1th field. If the -b option is in effect, n is counted from the first non-blank character in the m+1th field; m.0b refers to the first non-blank character in the m+1th field.

A - pos2 position specified by m.n is interpreted to mean the nth character (including separators) after the last character of the mth field. A missing.n means ‘.0’, indicating the last character of the mth field. If the -b option is in effect, n is counted from the last leading blank character in the m+1th field; m.1b refers to the first non-blank character in the m+1th field.

FILES
/var/tmp/stm*, /tmp/*
    Default temporary directories (in order of search).

SEE ALSO
comm(1), uniq(1), join(1)

DIAGNOSTICS

BUGS
Lines which are longer than 4096 are discarded and processing continues.

HISTORY
A sort command appeared in AT&T UNIX.