sort - Sort, merge, or sequence check text files

Synopsis

sort [-cmubdfinr] [-t char] [-T char] [-k keydef ...] [-o output] [file] ...

Description

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 performed lexicographically. By default, if keys are not given, sort regards each input line as a single field.

Options

-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. This option produces no output.
-m
Merge only; the input files are assumed to be presorted.
-o output
The output argument 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.
-u
Unique processing to 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, the ordering options override 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.
-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.
-r
Reverse the sense of comparisons.

The treatment of field separators can be altered using the 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
The char argument 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, "char-char" delimits an empty field). If -t is not specified, blank space characters are used as default field separators.
-T char
The char argument is used as the record separator character. This option should be used with discretion. The -T option with an alphanumeric char typically produces undesirable results. The default line separator is newline.
-k keydef
Select the key fields to use for sorting. keydef as the format:

field_start[type][,field_end[type]]

where field_start is the starting position and field_end is the optional ending position of a key field. If field_end is not specified, the ending position is the end of the line. The type is a character from the set of characters b, d, f, i, n, r. The type behaves the same as the corresponding option but only to the specified key field. If no -k option is specified, a default sort key is used. A maximum of nine -k options can be specified.

Operands

The path name of a file to be sorted, merged, or checked. If no file operands are specified, the standard input is used.

Extended description

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 field_start[type][,field_end[type]] option.

The field_start portion of the option argument has the form:

field_number[.first_character]

Fields and characters within fields are numbered starting with 1. The field_number and first_character are positive decimal integers and specify the first character to be used as part of a sort key. If .first_character is not specified, it refers to the first character of the field.

The field_end portion of the option argument has the form:

field_number[.last_character]

The field_number is a positive decimal integer and last_character is a non-negative decimal integer. If last_character is not specified or is zero, it refers to the last character of the field.

If the -b option or the b type modifier is in effect, characters in fields are counted from the first non-blank character.

Exit status

  • 0 normal behavior.
  • 1 on disorder (or non-uniqueness) with the -c option
  • 2 an error occurred