The sort program sorts and collates lines contained in the named files, and writes the result onto the standard output. If no filename argument is given, or if `-' appears as an argument, sort accepts input from the standard input.
Output lines are normally sorted on a character-by-character basis, from left to right within a line. The default collating sequence is the ASCII character set. Lines can also be sorted according to the contents of one or more fields specified by a sort-field, specification, using the +sw (starting-word), -ew (end-at-word), and the -tc (set-TAB-character/word delimiter) options, as described under OPTIONS below. When no word delimiter is specified, one or more adjacent white-space characters (SPACE and TAB) signify the end of the previous word; the lines:
^^^ xyz ^^^ xyz
are collated as:
^^^ xyz ^^^ xyz
Each sort-field is evaluated in command-line order; later fields are applied to the sorting sequence only when all earlier fields compare equally. When all specified fields compare equally between two or more lines, that subset of lines is sorted on a character-by-character basis, from left to right.
When no fields are specified in the command line, the System V version of sort treats leading blanks as significant, even with the -n (numeric collating sequence) option; the lines:
123 23
are collated as:
23 123
where sw is the number of the starting word (beginning with `0') to include in the field, ew is the number of the word before which to end the field, and cf is a string containing collating flags (without a leading `-'.) When included in a sort-field specification, these flags apply only to the field being specified, and when given, override other collating flags given in separate arguments (which otherwise apply to an entire line).
If the -ew option is omitted, the field continues to the end of a line.
You can apply a character offset to sw and ew to indicate that a field is to start or end a given number of characters within a word, using the notation: `w.c'. A starting position specified in the form: `+w.c' indicates the character in position c (beginning with 0 for the first character), within word w (1 and 1.0 are equivalent). An ending position specified in the form: `-w.c' indicates that the field ends at the character just prior to position c (beginning with 0 for the delimiter just prior to the first character), within word w. If the -b flag is in effect, c is counted from the first non-white-space or non-delimiter character in the field, otherwise, delimiter characters are counted.
Sort the contents of input-file with word number 1 (the second word) as the sort key:
sort +1 -2 input-file
Sort, in reverse order, the contents of input-file1 and input-file2, placing the output in output-file and using the first character of the second field as the sort key:
sort -r -o output-file +1.0 -1.1 input-file1 input-file2
Sort, in reverse order, the contents of input-file1 and input-file2 using the first non-blank character of the second field as the sort key:
sort -r +1.0b -1.1b input-file1 input-file2
Print the password file (passwd(5)) sorted by the numeric user ID (the third colon-separated field):
sort -t: +2n -3 /etc/passwd
Print the lines of the already sorted file input-file, suppressing all but the first occurrence of lines having the same third field (the options -mu with just one input file make the choice of a unique representative from a set of equal lines predictable):
sort -mu +2 -3 input-file
Comments and exits with non-zero status for various trouble conditions (such as when input lines are too long), and for disorders discovered under the -c option.
When the last line of an input file is missing a NEWLINE, sort appends one, prints a warning message, and continues.
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97