sort [-m][-o
output][-bdfinru][-t char][-k keydef]... [file...]
sort -c [-bdfinru][-t char][-k keydef][file]
The sort utility shall perform one of the following functions:
Sort lines of all the named files together and write the result to the specified output.
Merge lines of all the named (presorted) files together and write the result to the specified output.
Check that a single input file is correctly presorted.
Comparisons shall be based on one or more sort keys extracted from each line of input (or, if no sort keys are specified, the entire line up to, but not including, the terminating <newline>), and shall be performed using the collating sequence of the current locale.
The sort utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines, and the -k keydef option should follow the -b, -d, -f, -i, -n, and -r options.
The following options shall be supported:
The following options shall override the default ordering rules. When ordering options appear independent of any key field specifications, the requested field ordering rules shall be applied globally to all sort keys. When attached to a specific key (see -k), the specified ordering options shall override all global ordering options for that key.
The treatment of field separators can be altered using the options:
Sort keys can be specified using the options:
field_start[type][,field_end[type]]
where field_start and field_end define a key field restricted to a portion of the line (see the EXTENDED DESCRIPTION section), and type is a modifier from the list of characters ’b’ , ’d’ , ’f’ , ’i’ , ’n’ , ’r’ . The ’b’ modifier shall behave like the -b option, but shall apply only to the field_start or field_end to which it is attached. The other modifiers shall behave like the corresponding options, but shall apply only to the key field to which they are attached; they shall have this effect if specified with field_start, field_end, or both. If any modifier is attached to a field_start or to a field_end, no option shall apply to either. Implementations shall support at least nine occurrences of the -k option, which shall be significant in command line order. If no -k option is specified, a default sort key of the entire line shall be used.
When there are multiple key fields, later keys shall be compared only after all earlier keys compare equal. Except when the -u option is specified, lines that otherwise compare equal shall be ordered as if none of the options -d, -f, -i, -n, or -k were present (but with -r still in effect, if it was specified) and with all bytes in the lines significant to the comparison. The order in which lines that still compare equal are written is unspecified.
The following operand shall be supported:
The standard input shall be used only if no file operands are specified, or if a file operand is ’-’ . See the INPUT FILES section.
The input files shall be text files, except that the sort utility shall add a <newline> to the end of a file ending with an incomplete last line.
The following environment variables shall affect the execution of sort:
Determine the locale for ordering rules.
Determine the locale for the definition of the radix character and thousands separator for the -n option.
Default.
Unless the -o or -c options are in effect, the standard output shall contain the sorted input.
The standard error shall be used for diagnostic messages. A warning message about correcting an incomplete last line of an input file may be generated, but need not affect the final exit status.
If the -o option is in effect, the sorted input shall be written to the file output.
The notation:
-k field_start[type][,field_end[type]]
shall define a key field that begins at field_start and ends at field_end inclusive, unless field_start falls beyond the end of the line or after field_end, in which case the key field is empty. A missing field_end shall mean the last character of the line.
A field comprises a maximal sequence of non-separating characters and, in the absence of option -t, any preceding field separator.
The field_start portion of the keydef option-argument shall have the form:
field_number[.first_character]
Fields and characters within fields shall be numbered starting with 1. The field_number and first_character pieces, interpreted as positive decimal integers, shall specify the first character to be used as part of a sort key. If .first_character is omitted, it shall refer to the first character of the field.
The field_end portion of the keydef option-argument shall have the form:
field_number[.last_character]
The field_number shall be as described above for field_start. The last_character piece, interpreted as a non-negative decimal integer, shall specify the last character to be used as part of the sort key. If last_character evaluates to zero or .last_character is omitted, it shall refer to the last character of the field specified by field_number.
If the -b option or b type modifier is in effect, characters within a field shall be counted from the first non- <blank> in the field. (This shall apply separately to first_character and last_character.)
The following exit values shall be returned:
Default.
The following sections are informative.
The default value for -t, <blank>, has different properties from, for example, -t "<space>". If a line contains:
<space><space>foo
the following treatment would occur with default separation as opposed to specifically selecting a <space>:
The leading field separator itself is included in a field when -t |
is not used. For example, this command returns an exit |
status of zero, meaning the input was already sorted: |