SORT(1) General Commands Manual SORT(1)NAME
sort - sort or merge files
SYNOPSIS
sort [ -_________x ] [ +pos1 [ -pos2 ] ] ... [ -o name ] [ -T directory ] [ name ] ...
DESCRIPTION
Sort sorts lines of all the named files together and writes the result on the standard output. The name `-' means the standard input. If
no input files are named, the standard input is sorted.
The default sort key is an entire line. Default ordering is lexicographic by bytes in machine collating sequence. The ordering is
affected globally by the following options, one or more of which may appear.
b Ignore leading blanks (spaces and tabs) in field comparisons.
d `Dictionary' order: only letters, digits and blanks are significant in comparisons.
f Fold upper case letters onto lower case.
i Ignore characters outside the ASCII range 040-0176 in nonnumeric comparisons.
n An initial numeric string, consisting of optional blanks, optional minus sign, and zero or more digits with optional decimal point, is
sorted by arithmetic value. Option n implies option b.
r Reverse the sense of comparisons.
tx `Tab character' separating fields is x.
The notation +pos1 -pos2 restricts a sort key to a field beginning at pos1 and ending just before pos2. Pos1 and pos2 each have the form
m.n, optionally followed by one or more of the flags bdfinr, where m tells a number of fields to skip from the beginning of the line and n
tells a number of characters to skip further. If any flags are present they override all the global ordering options for this key. If the
b option is in effect n is counted from the first nonblank in the field; b is attached independently to pos2. A missing .n means .0; a
missing -pos2 means the end of the line. Under the -tx option, fields are strings separated by x; otherwise fields are nonempty nonblank
strings separated by blanks.
When there are multiple sort keys, later keys are compared only after all earlier keys compare equal. Lines that otherwise compare equal
are ordered with all bytes significant.
These option arguments are also understood:
c Check that the input file is sorted according to the ordering rules; give no output unless the file is out of sort.
m Merge only, the input files are already sorted.
o The next argument is the name of an output file to use instead of the standard output. This file may be the same as one of the
inputs.
T The next argument is the name of a directory in which temporary files should be made.
u Suppress all but one in each set of equal lines. Ignored bytes and bytes outside keys do not participate in this comparison.
Examples. Print in alphabetical order all the unique spellings in a list of words. Capitalized words differ from uncapitalized.
sort -u +0f +0 list
Print the password file (passwd(5)) sorted by user id number (the 3rd colon-separated field).
sort -t: +2n /etc/passwd
Print the first instance of each month in an already sorted file of (month day) entries. The options -um with just one input file make the
choice of a unique representative from a set of equal lines predictable.
sort -um +0 -1 dates
FILES
/usr/tmp/stm*, /tmp/*: first and second tries for temporary files
SEE ALSO uniq(1), comm(1), rev(1), join(1)DIAGNOSTICS
Comments and exits with nonzero status for various trouble conditions and for disorder discovered under option -c.
BUGS
Very long lines are silently truncated.
SORT(1)
Check Out this Related Man Page
sort5(1) General Commands Manual sort5(1)Name
sort5 - internationalized System 5 sort and/or merge files
Syntax
sort5 [-cmu] [-ooutput] [-ykmem] [-zrecsz] [-X] [-dfiMnr] [-btx] [+pos1 [-pos2]] [files]
Description
The command sorts lines of the named files together and writes the result on the standard output. The standard input is read if a hyphen
(-) is used as a file name or if no input files are named.
Comparisons are based on one or more sort keys extracted from each line of input. By default, there is one sort key, the entire input
line, and ordering is determined by the collating sequence specified by the LC_COLLATE locale. The LC_COLLATE locale is controlled by the
settings of either the LANG or LC_COLLATE environment variables. See for more information.
Options
The following options alter the default behavior:
-c Checks that the input file is sorted according to the ordering rules; gives no output unless the file is out of order.
-m Merges only; the input files are already sorted.
-u Suppresses all but one in each set of lines having equal keys.
-ooutput
Specifies the name of an output file to use instead of the standard output. The file may be the same as one of the inputs. Blanks
between -o and output are optional.
-ykmem
Specifies the number of kilobytes of memory to use when sorting a file. If this option is omitted, sort5 begins using a system
default memory size, and continues to use more space as needed. If kmem is specified, sort5 starts using that number of kilobytes of
memory. If the administrative minimum or maximum is violated, the value of the corresponding minimum or maximum is used. Thus, -y0
is guaranteed to start with minimum memory. By convention, -y (with no argument) starts with maximum memory.
-zrecsz
Records the size of the longest line read in the sort phase so buffers can be allocated during the merge phase. If the sort phase is
omitted using either the -c or -m options, a system default size is used. Lines longer than the buffer size cause to terminate abnor-
mally. Supplying the actual number of bytes (or some larger value) in the longest line to be merged prevents abnormal termination.
-X Sorts using tags. Upon input each key is converted to a tag value which is sorted efficiently. This option makes international sorting
faster but it consumes more memory since both key and tag must be stored.
The following options override the default ordering rules:
-d Specifies Dictionary order. Only letters, digits and blanks (spaces and tabs) are significant in comparisons.
-f Folds lower case letters into upper case.
-i Ignores characters outside the ASCII range 040-0176 in non-numeric comparisons.
-n Sorts an initial numeric string, consisting of optional blanks, optional minus sign, and zero or more digits with optional decimal
point, by arithmetic value. The -n option implies the -b option, which tells the command to ignore leading blanks when determining
the starting and ending positions of a restricted sort key.
-r Reverses the sense of comparisons.
When ordering options appear before restricted sort key specifications, the requested ordering rules are applied globally to all sort keys.
When attached to a specific sort key (described below), the specified ordering options override all global ordering options for that key.
The notation +pos1 -pos2 restricts a sort key to one beginning at pos1 and ending at pos2. The characters at positions pos1 and pos2 are
included in the sort key (provided that pos2 does not precede pos1). A missing -pos2 means the end of the line.
Specifying pos1 and pos2 involves the notion of a field, that is a minimal sequence of characters followed by a field separator or a new-
line. By default, the first blank of a sequence of blanks acts as the field separator. The blank can be either a space or a tab. All
blanks in a sequence of blanks are interpreted as a part of the next field; for example, all blanks at the beginning of a line are consid-
ered to be part of the first field. The treatment of field separators is altered using the following options:
-tx Uses x as the field separator character. Although it may be included in a sort key, x is not considered part of a field. Each occur-
rence of x is significant (for example, xx delimits an empty field).
-b Ignores leading blanks when determining the starting and ending positions of a restricted sort key. If the -b option is specified
before the first +pos1 argument, it is applied to all +pos1 arguments. Otherwise, the b flag may be attached independently to each
+pos1 or -pos2 argument.
Pos1 and pos2 each have the form m.n optionally followed by one or more of the flags bdfinr. A starting position specified by +m.n is
interpreted to mean the n+1st character in the m+1st field. A missing .n means .0, indicating the first character of the m+1st field. If
the b flag is in effect n is counted from the first non-blank in the m+1st field; +m.0b refers to the first non-blank character in the
m+1st field.
A last position specified by -m.n is interpreted to mean the nth character (including separators) after the last character of the m th
field. A missing .n means .0, indicating the last character of the mth field. If the b flag is in effect n is counted from the last lead-
ing blank in the m+1st field; -m.1b refers to the first non-blank in the m+1st field.
When there are multiple sort keys, later keys are compared only after all earlier keys are found to be equal. Lines that otherwise compare
equal are ordered with all bytes significant.
Examples
Sort the contents of infile with the second field as the sort key:
sort5 +1 -2 infile
Sort, in reverse order, the contents of infile1 and infile2, placing the output in outfile and using the first character of the second
field as the sort key:
sort5 -r -o outfile +1.0 -1.2 infile1 infile2
Sort, in reverse order, the contents of infile1 and infile2 using the first non-blank character of the second field as the sort key:
sort5 -r +1.0b -1.1b infile1 infile2
Print the password file sorted by the numeric user ID (the third colon-separated field):
sort5 -t: +2n -3 /etc/passwd
Print the lines of the already sorted file infile, suppressing all but the first occurrence of lines having the same third field (the
options -um with just one input file make the choice of a unique representative from a set of equal lines predictable):
sort5 -um +2 -3 infile
Diagnostics
Comments and exits with non-zero status for various trouble conditions (for example, when input lines are too long), and for disorder dis-
covered under the -c option.
When the last line of an input file is missing a new-line character, sort5 appends one, prints a warning message, and continues.
Files
/usr/tmp/stm???
See Alsocomm(1), join(1), uniq(1), setlocale(3int), strcoll(3int)sort5(1)