SORT(1) General Commands Manual SORT(1)
sort - sort or merge files
sort [ -_________x ] [ +pos1 [ -pos2 ] ] ... [ -o name ] [ -T directory ] [ name ] ...
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
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.
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
/usr/tmp/stm*, /tmp/* first and second tries for temporary files
uniq(1), comm(1), rev(1), join(1)
Comments and exits with nonzero status for various trouble conditions and for disorder discovered under option -c.
Very long lines are silently truncated.
7th Edition April 29, 1985 SORT(1)
Check Out this Related Man Page
sort(1) General Commands Manual sort(1)
sort - sort or merge files
output] char] keydef] [kmem]] recsz] dir] [file ...]
char] keydef] [kmem]] recsz] dir] [file ...]
performs one of the following functions:
1. Sorts lines of all the named files together and writes the result to the specified output.
2. Merges lines of all the named (presorted) files together and writes the result to the specified output.
3. Checks that a single input file is correctly presorted.
The standard input is read if is used as a file name or no input files are specified.
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. Ordering is lexicographic by characters using the collating sequence of the current locale. If the locale is not specified or is
set to the locale, then ordering is lexicographic by bytes in machine-collating sequence. If the locale includes multi-byte characters,
single-byte characters are machine-collated before multi-byte characters.
Behavior Modification Options
The following options alter the default behavior:
Sorts on a byte-by-byte basis using each character's encoded value.
On some systems, extended characters will be considered negative values, and so sort before ASCII characters. If you
are sorting ASCII characters in a non-C/POSIX locale, this flag performs much faster.
Check that the single input file is sorted according to the ordering rules.
No output is produced; the exit code is set to indicate the result.
Merge only; the input files are assumed to be already sorted.
The argument given is the name of an output file
to use instead of the standard output. This file can be the same as one of the input files.
Unique: suppress all but one in each
set of lines having equal keys. If used with the option, check to see that there are no lines with duplicate keys, in
addition to checking that the input file is sorted.
The amount of main memory used by the sort
can have a large impact on its performance. If this option is omitted, begins using a system default memory size, and
continues to use more space as needed. If this option is presented with a value, kmem, starts using that number of
kilobytes of memory, unless the administrative minimum or maximum is violated, in which case the corresponding extremum
will be used. Thus, is guaranteed to start with minimum memory. By convention, (with no argument) starts with maximum
The size of the longest line read is recorded
in the sort phase so that buffers can be allocated during the merge phase. If the sort phase is omitted via the or
options, a popular system default size will be used. Lines longer than the buffer size will cause to terminate abnor-
mally. Supplying the actual number of bytes in the longest line to be merged (or some larger value) will prevent abnor-
Use dir as the directory for temporary scratch files rather than the default directory, which is is one of the following,
tried in order: the directory as specified in the environment variable; and finally,
Ordering Rule Options
When ordering options appear before restricted sort key specifications, the ordering rules are applied globally to all sort keys. When
attached to a specific sort key (described below), the ordering options override all global ordering options for that key.
The following options override the default ordering rules:
only alphanumeric characters and blanks (spaces and tabs), as defined by are significant in comparisons (see envi-
(UNIX Standard only, see standards(5)) The behavior is undefined for a sort key to which -i or -n also applies.
Prior to being compared, all lowercase letters are effectively converted into their uppercase equivalents, as defined by
In non-numeric comparisons, ignore all characters which are non-printable,
as defined by For the ASCII character set, octal character codes 001 through 037 and 0177 are ignored.
The sort key is restricted to
an initial numeric string consisting of optional blanks, an optional minus sign, zero or more digits with optional radix
character, and optional thousands separators. The radix and thousands separator characters are defined by The field is
sorted by arithmetic value. An empty (missing) numeric field is treated as arithmetic zero. Leading zeros and plus or
minus signs on zeros do not affect the ordering. The option implies the option (see below).
Reverse the sense of comparisons.
Compare as months.
The first several non-blank characters of the field are folded to uppercase and compared with the langinfo(5) items < <
... < An invalid field is treated as being less than string. For example, American month names are compared such that <
< ... < An invalid field is treated as being less than all months. The option implies the option (see below).
Field Separator Options
The treatment of field separators can be altered using the options:
Use char 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, <char><char> delimits an empty field). If is not spec-
ified, <blank> characters will be used as default field separators; each maximal sequence of <blank> characters that
follows a non-<blank> character is a field separator.
Ignore leading blanks when determining the starting and ending
positions of a restricted sort key. If the option is specified before the first option pos1 argument), it is applied to
all options pos1 arguments). Otherwise, the option can be attached independently to each field_start or field_end
option pos1 or pos2 argument; see below). Note that the option is only effective when restricted sort key specifica-
tions are given.
Restricted Sort Key
The keydef argument defines a restricted sort key. The format of this definition is
which defines a key field beginning at field_start and ending at field_end. The characters at positions field_start and
field_end are included in the key field, providing that field_end does not precede field_start. A missing field_end
means the end of the line. Fields and characters within fields are numbered starting with Note that this is different
than the obsolete form of restricted sort keys, where numbering starts at See below.
Specifying field_start and field_end involves the notion of a field, 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. All
blanks in a sequence of blanks are considered to be part of the next field; for example, all blanks at the beginning of
a line are considered to be part of the first field.
The arguments field_start and field_end each have the form which are optionally followed by one or more of the type
options or These modifiers have the functionality for this key only, that their command-line counterparts have for the
A field_start position specified by is interpreted to mean the nth character in the mth field. A missing n means indi-
cating the first character of the mth field. If the option is in effect, n is counted from the first non-blank charac-
ter in the mth field.
A field_end position specified by is interpreted to mean the nth character in the mth field. If n is missing, the mth
field ends at the last character of the field. If the option is in effect, n is counted from the first non-<blank>
character in the mth field.
Multiple options are permitted and are significant in command line order. A maximum of 9 options can be given. If no
option is specified, a default sort key of the entire line is used. 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 sig-
nificant. If all the specified keys compare equal, the entire record is used as the final key.
The option is intended to replace the obsolete pos1 pos2]] notation, using field_start and field_end respectively. The
fully specified pos1 pos2]] form:
is equivalent to:
-k w+1.x+1,y.0 (if z == 0)
-k w+1.x+1,y+1.z (if z > 0)
Obsolete Restricted Sort Key
The notation 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 means the end of the line.
Specifying pos1 and pos2 involves the notion of a field, a minimal sequence of characters followed by a field separator or a new-line. By
default, the first blank (space or tab) of a sequence of blanks acts as the field separator. All blanks in a sequence of blanks are con-
sidered to be part of the next field; for example, all blanks at the beginning of a line are considered to be part of the first field.
pos1 and pos2 each have the form optionally followed by one or more of the flags A starting position specified by is interpreted to mean
character n+1 in field m+1. A missing means indicating the first character of field m+1. If the flag is in effect, n is counted from the
first non-blank in field m+1; refers to the first non-blank character in field m+1.
A last position specified by is interpreted to mean the nth character (including separators) after the last character of the mth field. A
missing means indicating the last character of the mth field. If the flag is in effect, n is counted from the last leading blank in field
m+1; refers to the first non-blank in field m+1.
For information about the UNIX standard environment, see standards(5).
determines the default ordering rules applied to the sort.
determines the locale for interpretation of sequences of bytes of text data as characters (e.g., single- verses multibyte characters in
arguments and input files) and the behavior of character classification for the and options.
determines the definition of the radix and thousands separator characters for the option.
determines the month names for the option.
determines the language in which messages are displayed.
determines the locale to use to override the values of all the other internationalization variables.
determines the location of message catalogs for the processing of
provides a default value for the internationalization variables that are unset or null. If is unset or null, the default value of "C" (see
lang(5)) is used.
If any of the internationalization variables contains an invalid setting, behaves as if all internationalization variables are set to "C".
International Code Set Support
Single- and multi-byte character code sets are supported.
Sort the contents of with the second field as the sort key:
Sort, in reverse order, the contents of and placing the output in and using the first two characters of the second field as the sort key:
Sort, in reverse order, the contents of and using the first non-blank character of the fourth field as the sort key:
Print the password file sorted by numeric user ID (the third colon-separated field):
Print the lines of the presorted file suppressing all but the first occurrence of lines having the same third field:
exits with one of the following values:
All input files were output successfully, or
was specified and the input file was correctly presorted.
option, the file was not ordered as specified, or if the and options were both specified, two input lines were found with equal
keys. This exit status is not returned if the option is not used.
An error occurred such as when one or more input lines are too long.
When the last line of an input file is missing a new-line character, appends one, prints a warning message, and continues.
If an error occurs when accessing the tables that contain the collation rules for the specified language, prints a warning message and
defaults to the locale.
If a or option is specified for a language with multi-byte characters, prints a warning message and ignores the option.
Numbering of fields and characters within fields option) has changed to conform to the POSIX standard. Beginning at HP-UX Release 9.0, the
option numbers fields and characters within fields, starting with Prior to HP-UX Release 9.0, numbering started at
A field separator specified by the option is recognized only if it is a single-byte character.
The character type classification categories and are not defined for multi-byte characters. For languages with multi-byte characters, all
characters are significant in comparisons.
For non-text input files, the behaviour is undefined.
was developed by OSF and HP.
comm(1), join(1), uniq(1), environ(5), lang(5), standards(5).