User Commands sort(1) NAME sort - sort, merge, or sequence check text files SYNOPSIS /usr/bin/sort [ -cmu ] [ -o _o_u_t_p_u_t ] [ -T _d_i_r_e_c_t_o_r_y ] [ -y [ _k_m_e_m ]] [ -z _r_e_c_s_z ] [ -dfiMnr ] [ -b ] [ -t _c_h_a_r ] [ -k _k_e_y_d_e_f ] [ +_p_o_s_1 [ -_p_o_s_2 ]] [ _f_i_l_e...] /usr/xpg4/bin/sort [ -cmu ] [ -o _o_u_t_p_u_t ] [ -T _d_i_r_e_c_t_o_r_y ] [ -y [ _k_m_e_m ]] [ -z _r_e_c_s_z ] [ -dfiMnr ] [ -b ] [ -t _c_h_a_r ] [ -k _k_e_y_d_e_f ] [ +_p_o_s_1 [ -_p_o_s_2 ]] [ _f_i_l_e...] DESCRIPTION The sort command sorts lines of all the named files together and writes the result on the standard output. 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. Lines are ordered according to the collating sequence of the current locale. OPTIONS The following options alter the default behavior: /usr/bin/sort -c Check that the single input file is ordered as specified by the arguments and the collating sequence of the current locale. The exit code is set and no output is produced unless the file is out of sort. /usr/xpg4/bin/sort -c Same as /usr/bin/sort except no output is produced under any circumstances. -m Merge only. The input files are assumed to be already sorted. -u Unique: 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 in addition to checking that the input file is sorted. -o _o_u_t_p_u_t Specify 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. -T _d_i_r_e_c_t_o_r_y The _d_i_r_e_c_t_o_r_y argument is the name of a directory SunOS 5.6 Last change: 18 Mar 1997 1 User Commands sort(1) in which to place temporary files. -y _k_m_e_m The amount of main memory initially used by sort. If this option is omitted, sort begins using a system default memory size, and continues to use more space as needed. If _k_m_e_m is present, sort will start using that number of Kbytes of memory, unless the administrative minimum or maximum is exceeded, in which case the corresponding extremum will be used. Thus, -y 0 is guaranteed to start with minimum memory. -y with no _k_m_e_m argument starts with maximum memory. -z _r_e_c_s_z (obsolete). This option was used to prevent abnormal termination when lines longer than the system-dependent default buffer size are encoun- tered. Because sort automatically allocates buffers large enough to hold the longest line, this option has no effect. Ordering Options The following options override the default ordering rules. When ordering options appear independent of any key field specifications, the requested field ordering rules are applied globally to all sort keys. When attached to a specific key (see Sort Key Options), the specified ordering options override all global ordering options for that key. In the obsolescent forms, if one or more of these options follows a +_p_o_s_1 option, it will affect only the key field specified by that preceding option. -d ``Dictionary'' order: only letters, digits, and blanks (spaces and tabs) are significant in com- parisons. -f Fold lower-case letters into upper case. -i Ignore non-printable characters. -M Compare as months. The first three non-blank characters of the field are folded to upper case and compared. For example, in English the sorting order is "JAN" < "FEB" < ... < "DEC". Invalid fields compare low to "JAN". The -M option implies the -b option (see below). -n Restrict the sort key to an initial numeric string, consisting of optional blank characters, optional minus sign, and zero or more digits with an optional radix character and thousands separa- tors (as defined in the current locale), which will be sorted by arithmetic value. An empty SunOS 5.6 Last change: 18 Mar 1997 2 User Commands sort(1) digit string is treated as zero. Leading zeros and signs on zeros do not affect ordering. -r Reverse the sense of comparisons. Field Separator Options The treatment of field separators can be altered using the following options: -b Ignore leading blank characters when determining the starting and ending positions of a restricted sort key. If the -b option is specified before the first sort key option, it is applied to all sort key options. Otherwise, the -b option can be attached independently to each -k _f_i_e_l_d__s_t_a_r_t, _f_i_e_l_d__e_n_d, or +_p_o_s_1 or -_p_o_s_2 option-argument (see below). -t _c_h_a_r Use _c_h_a_r as the field separator character. _c_h_a_r is not considered to be part of a field (although it can be included in a sort key). Each occurrence of _c_h_a_r is significant (for example, <_c_h_a_r><_c_h_a_r> delimits an empty field). If -t is not specified, blank characters are used as default field separators; each maximal non-empty sequence of blank characters that follows a non- blank character is a field separator. Sort Key Options Sort keys can be specified using the options: -k _k_e_y_d_e_f The _k_e_y_d_e_f argument is a restricted sort key field definition. The format of this definition is: -k _f_i_e_l_d__s_t_a_r_t [ _t_y_p_e ] [ ,_f_i_e_l_d__e_n_d [ _t_y_p_e ] ] where: _f_i_e_l_d__s_t_a_r_t and _f_i_e_l_d__e_n_d define a key field restricted to a por- tion of the line. _t_y_p_e is a modifier from the list of charac- ters bdfiMnr. The b modifier behaves like the -b option, but applies only to the _f_i_e_l_d__s_t_a_r_t or _f_i_e_l_d__e_n_d to which it is attached and characters within a field are counted from the first non- blank character in the field. (This applies separately to _f_i_r_s_t__c_h_a_r_a_c_t_e_r and _l_a_s_t__c_h_a_r_a_c_t_e_r.) The other modif- iers behave like the corresponding options, but apply only to the key field SunOS 5.6 Last change: 18 Mar 1997 3 User Commands sort(1) to which they are attached. They have this effect if specified with _f_i_e_l_d__s_t_a_r_t, _f_i_e_l_d__e_n_d or both. If any modifier is attached to a _f_i_e_l_d__s_t_a_r_t or to a _f_i_e_l_d__e_n_d, no option applies to either. When there are multiple key fields, later keys are compared only after all earlier keys compare equal. Except when the -u option is specified, lines that otherwise compare equal are 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 signi- ficant to the comparison. The notation: -k _f_i_e_l_d__s_t_a_r_t[_t_y_p_e][,_f_i_e_l_d__e_n_d[_t_y_p_e]] defines a key field that begins at _f_i_e_l_d__s_t_a_r_t and ends at _f_i_e_l_d__e_n_d inclusive, unless _f_i_e_l_d__s_t_a_r_t falls beyond the end of the line or after _f_i_e_l_d__e_n_d, in which case the key field is empty. A missing _f_i_e_l_d__e_n_d means 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 _f_i_e_l_d__s_t_a_r_t portion of the _k_e_y_d_e_f option- argument has the form: _f_i_e_l_d__n_u_m_b_e_r[._f_i_r_s_t__c_h_a_r_a_c_t_e_r] Fields and characters within fields are numbered starting with 1. _f_i_e_l_d__n_u_m_b_e_r and _f_i_r_s_t__c_h_a_r_a_c_t_e_r, interpreted as positive decimal integers, specify the first character to be used as part of a sort key. If ._f_i_r_s_t__c_h_a_r_a_c_t_e_r is omitted, it refers to the first character of the field. The _f_i_e_l_d__e_n_d portion of the _k_e_y_d_e_f option- argument has the form: _f_i_e_l_d__n_u_m_b_e_r[._l_a_s_t__c_h_a_r_a_c_t_e_r] The _f_i_e_l_d__n_u_m_b_e_r is as described above for _f_i_e_l_d__s_t_a_r_t. _l_a_s_t__c_h_a_r_a_c_t_e_r, interpreted as a non-negative decimal integer, specifies the last SunOS 5.6 Last change: 18 Mar 1997 4 User Commands sort(1) character to be used as part of the sort key. If _l_a_s_t__c_h_a_r_a_c_t_e_r evaluates to zero or ._l_a_s_t__c_h_a_r_a_c_t_e_r is omitted, it refers to the last character of the field specified by _f_i_e_l_d__n_u_m_b_e_r. If the -b option or b type modifier is in effect, characters within a field are counted from the first non-blank character in the field. (This applies separately to _f_i_r_s_t__c_h_a_r_a_c_t_e_r and _l_a_s_t__c_h_a_r_a_c_t_e_r.) [+_p_o_s_1[-_p_o_s_2]] (obsolete). Provide functionality equivalent to the -k _k_e_y_d_e_f option. _p_o_s_1 and _p_o_s_2 each have the form _m._n optionally followed by one or more of the flags bdfiMnr. 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 charac- ter 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 _mth 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 leading blank in the _m+1st field; -_m.1b refers to the first non-blank in the _m+1st field. The fully specified +_p_o_s_1 -_p_o_s_2 form with type modifiers T and U: +w.xT -y.zU is equivalent to: undefined (z==0 & U contains _b & -_t is present) -k w+1.x+1T,y.0U (z==0 otherwise) -k w+1.x+1T,y+1.zU (z > 0) Implementations support at least nine occurrences of the sort keys (the -k option and obsolescent +_p_o_s_1 and -_p_o_s_2) which are significant in command line order. If no sort key is specified, a default sort key of the entire line is used. OPERANDS The following operand is supported: SunOS 5.6 Last change: 18 Mar 1997 5 User Commands sort(1) _f_i_l_e A path name of a file to be sorted, merged or checked. If no _f_i_l_e operands are specified, or if a _f_i_l_e operand is -, the standard input will be used. USAGE See largefile(5) for the description of the behavior of sort when encountering files greater than or equal to 2 Gbyte (2**_3_1 bytes). EXAMPLES In the following examples, non-obsolescent and obsolescent ways of specifying sort keys are given as an aid to under- standing the relationship between the two forms. Either of the following commands sorts the contents of infile with the second field as the sort key: example% sort -k 2,2 infile example% sort +1 -2 infile Either of the following commands sorts, in reverse order, the contents of infile1 and infile2, placing the output in outfile and using the second character of the second field as the sort key (assuming that the first character of the second field is the field separator): example% sort -r -o outfile -k 2.2,2.2 infile1 infile2 example% sort -r -o outfile +1.1 -1.2 infile1 infile2 Either of the following commands sorts the contents of infile1 and infile2 using the second non-blank character of the second field as the sort key: example% sort -k 2.2b,2.2b infile1 infile2 example% sort +1.1b -1.2b infile1 infile2 Either of the following commands prints the passwd(4) file (user database) sorted by the numeric user ID (the third colon-separated field): example% sort -t : -k 3,3n /etc/passwd example% sort -t : +2 -3n /etc/passwd Either of the following commands prints the lines of the already sorted file infile, suppressing all but one occurrence of lines having the same third field: example% sort -um -k 3.1,3.0 infile example% sort -um +2.0 -3.0 infile SunOS 5.6 Last change: 18 Mar 1997 6 User Commands sort(1) ENVIRONMENT See environ(5) for descriptions of the following environment variables that affect the execution of sort: LC_COLLATE, LC_MESSAGES, and NLSPATH. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as charac- ters (for example, single- versus multi-byte characters in arguments and input files) and the behavior of character classification for the -b, -d, -f, -i and -n options. LC_NUMERIC Determine the locale for the definition of the radix character and thousands separator for the -n option. EXIT STATUS The following exit values are returned: 0 All input files were output successfully, or -c was specified and the input file was correctly sorted. 1 Under the -c option, the file was not ordered as specified, or if the -c and -u options were both specified, two input lines were found with equal keys. >1 An error occurred. FILES /var/tmp/stm??? temporary files ATTRIBUTES See attributes(5) for descriptions of the following attri- butes: /usr/bin/sort __________________________________ | ATTRIBUTE TYPE| ATTRIBUTE VALUE| |_______________________________|____________________________________|_ | Availability | SUNWesu | | CSI | Enabled | |________________|__________________| /usr/xpg4/bin/sort __________________________________ ATTRIBUTE TYPE ATTRIBUTE VALUE ____________________________________________________________________ Availability SUNWxcu4 CSI Enabled __________________________________ SunOS 5.6 Last change: 18 Mar 1997 7 User Commands sort(1) | | | SEE |ALSO | | |comm(1), join|(1), uniq(1), |passwd(4), attributes(5), |environ(5), lar|gefile(5), xpg4(5)| | | | DIAG|NOSTICS | | |Comments and ex|its with non-zero |status for various trouble |conditions (for|example, when inp|ut lines are too long), and |for disorders d|iscovered under th|e -c option. | | | NOTE|S | | |When the last l|ine of an input fi|le is missing a new-line |character, sor|t appends one, pr|ints a warning message, and |continues. | | | | | |sort does not g|uarantee preservat|ion of relative line order- |ing on equal ke|ys. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | SunOS 5.6 Last change: 18 Mar 1997 8