SORT(1)                    POSIX Programmer's Manual                   SORT(1)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       sort -- sort, merge, or sequence check text files

SSYYNNOOPPSSIISS
       sort [[-m]] [[-o _o_u_t_p_u_t]] [[-bdfinru]] [[-t _c_h_a_r]] [[-k _k_e_y_d_e_f]]... [[_f_i_l_e...]]

       sort [[-c|-C]] [[-bdfinru]] [[-t _c_h_a_r]] [[-k _k_e_y_d_e_f]] [[_f_i_l_e]]

DDEESSCCRRIIPPTTIIOONN
       The _s_o_r_t utility shall perform one of the following functions:

        1. Sort lines of all the named files together and write the result to
           the specified output.

        2. Merge lines of all the named (presorted) files together and write
           the result to the specified output.

        3. 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. If this
       collating sequence does not have a total ordering of all characters
       (see the Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _7_._3_._2,
       _L_C___C_O_L_L_A_T_E), any lines of input that collate equally should be further
       compared byte-by-byte using the collating sequence for the POSIX
       locale.

OOPPTTIIOONNSS
       The _s_o_r_t utility shall conform to the Base Definitions volume of
       POSIX.1-2017, _S_e_c_t_i_o_n _1_2_._2, _U_t_i_l_i_t_y _S_y_n_t_a_x _G_u_i_d_e_l_i_n_e_s, except for
       Guideline 9, and the --kk _k_e_y_d_e_f option should follow the --bb, --dd, --ff, --ii,
       --nn, and --rr options. In addition, ''++'' may be recognized as an option
       delimiter as well as ''--''.

       The following options shall be supported:

       --cc        Check that the single input file is ordered as specified by
                 the arguments and the collating sequence of the current
                 locale. Output shall not be sent to standard output. The exit
                 code shall indicate whether or not disorder was detected or
                 an error occurred. If disorder (or, with --uu, a duplicate key)
                 is detected, a warning message shall be sent to standard
                 error indicating where the disorder or duplicate key was
                 found.

       --CC        Same as --cc, except that a warning message shall not be sent
                 to standard error if disorder or, with --uu, a duplicate key is
                 detected.

       --mm        Merge only; the input file shall be assumed to be already
                 sorted.

       --oo _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 _f_i_l_es.

       --uu        Unique: suppress all but one in each set of lines having
                 equal keys.  If used with the --cc option, check that there are
                 no lines with duplicate keys, in addition to checking that
                 the input file is sorted.

       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 --kk), the specified
       ordering options shall override all global ordering options for that
       key.

       --dd        Specify that only <blank> characters and alphanumeric
                 characters, according to the current setting of _L_C___C_T_Y_P_E,
                 shall be significant in comparisons. The behavior is
                 undefined for a sort key to which --ii or --nn also applies.

       --ff        Consider all lowercase characters that have uppercase
                 equivalents, according to the current setting of _L_C___C_T_Y_P_E, to
                 be the uppercase equivalent for the purposes of comparison.

       --ii        Ignore all characters that are non-printable, according to
                 the current setting of _L_C___C_T_Y_P_E.  The behavior is undefined
                 for a sort key for which --nn also applies.

       --nn        Restrict the sort key to an initial numeric string,
                 consisting of optional <blank> characters, optional <hyphen-
                 minus> character, and zero or more digits with an optional
                 radix character and thousands separators (as defined in the
                 current locale), which shall be sorted by arithmetic value.
                 An empty digit string shall be treated as zero. Leading zeros
                 and signs on zeros shall not affect ordering.

       --rr        Reverse the sense of comparisons.

       The treatment of field separators can be altered using the options:

       --bb        Ignore leading <blank> characters when determining the
                 starting and ending positions of a restricted sort key. If
                 the --bb option is specified before the first --kk option, it
                 shall be applied to all --kk options. Otherwise, the --bb option
                 can be attached independently to each --kk _f_i_e_l_d___s_t_a_r_t or
                 _f_i_e_l_d___e_n_d option-argument (see below).

       --tt _c_h_a_r   Use _c_h_a_r as the field separator character; _c_h_a_r shall not be
                 considered to be part of a field (although it can be included
                 in a sort key). Each occurrence of _c_h_a_r shall be significant
                 (for example, <_c_h_a_r><_c_h_a_r> delimits an empty field). If --tt is
                 not specified, <blank> characters shall be used as default
                 field separators; each maximal non-empty sequence of <blank>
                 characters that follows a non-<blank> shall be a field
                 separator.

       Sort keys can be specified using the options:

       --kk _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:


                     _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 portion of the line (see the EXTENDED DESCRIPTION
                 section), and _t_y_p_e is one or more modifiers from the list of
                 characters ''bb'', ''dd'', ''ff'', ''ii'', ''nn'', ''rr''.  The ''bb'' modifier
                 shall behave like the --bb option, but shall apply 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. 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 _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 shall apply to
                 either. Implementations shall support at least nine
                 occurrences of the --kk option, which shall be significant in
                 command line order. If no --kk 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 --uu option is specified, lines that otherwise compare
                 equal shall be ordered as if none of the options --dd, --ff, --ii,
                 --nn, or --kk were present (but with --rr 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.

OOPPEERRAANNDDSS
       The following operand shall be supported:

       _f_i_l_e      A pathname 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 shall be used. If _s_o_r_t encounters an error
                 when opening or reading a _f_i_l_e operand, it may exit without
                 writing any output to standard output or processing later
                 operands.

SSTTDDIINN
       The standard input shall be used only if no _f_i_l_e operands are
       specified, or if a _f_i_l_e operand is ''--''.  See the INPUT FILES section.

IINNPPUUTT FFIILLEESS
       The input files shall be text files, except that the _s_o_r_t utility shall
       add a <newline> to the end of a file ending with an incomplete last
       line.

EENNVVIIRROONNMMEENNTT VVAARRIIAABBLLEESS
       The following environment variables shall affect the execution of _s_o_r_t:

       _L_A_N_G      Provide a default value for the internationalization
                 variables that are unset or null. (See the Base Definitions
                 volume of POSIX.1-2017, _S_e_c_t_i_o_n _8_._2, _I_n_t_e_r_n_a_t_i_o_n_a_l_i_z_a_t_i_o_n
                 _V_a_r_i_a_b_l_e_s for the precedence of internationalization
                 variables used to determine the values of locale categories.)

       _L_C___A_L_L    If set to a non-empty string value, override the values of
                 all the other internationalization variables.

       _L_C___C_O_L_L_A_T_E
                 Determine the locale for ordering rules.

       _L_C___C_T_Y_P_E  Determine the locale for the interpretation of sequences of
                 bytes of text data as characters (for example, single-byte as
                 opposed to multi-byte characters in arguments and input
                 files) and the behavior of character classification for the
                 --bb, --dd, --ff, --ii, and --nn options.

       _L_C___M_E_S_S_A_G_E_S
                 Determine the locale that should be used to affect the format
                 and contents of diagnostic messages written to standard
                 error.

       _L_C___N_U_M_E_R_I_C
                 Determine the locale for the definition of the radix
                 character and thousands separator for the --nn option.

       _N_L_S_P_A_T_H   Determine the location of message catalogs for the processing
                 of _L_C___M_E_S_S_A_G_E_S.

AASSYYNNCCHHRROONNOOUUSS EEVVEENNTTSS
       Default.

SSTTDDOOUUTT
       Unless the --oo or --cc options are in effect, the standard output shall
       contain the sorted input.

SSTTDDEERRRR
       The standard error shall be used for diagnostic messages. When --cc is
       specified, if disorder is detected (or if --uu is also specified and a
       duplicate key is detected), a message shall be written to the standard
       error which identifies the input line at which disorder (or a duplicate
       key) was detected. A warning message about correcting an incomplete
       last line of an input file may be generated, but need not affect the
       final exit status.

OOUUTTPPUUTT FFIILLEESS
       If the --oo option is in effect, the sorted input shall be written to the
       file _o_u_t_p_u_t.

EEXXTTEENNDDEEDD DDEESSCCRRIIPPTTIIOONN
       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]]]]

       shall define 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 shall mean the last character of the line.

       A field comprises a maximal sequence of non-separating characters and,
       in the absence of option --tt, 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 shall have 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 shall be numbered starting with 1.
       The _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 pieces, interpreted as positive
       decimal integers, shall 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 shall refer 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 shall have 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 shall be as described above for _f_i_e_l_d___s_t_a_r_t_.  The
       _l_a_s_t___c_h_a_r_a_c_t_e_r piece, interpreted as a non-negative decimal integer,
       shall specify the last 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
       shall refer to the last character of the field specified by
       _f_i_e_l_d___n_u_m_b_e_r.

       If the --bb option or bb 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 _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.)

EEXXIITT SSTTAATTUUSS
       The following exit values shall be returned:

        0    All input files were output successfully, or --cc was specified and
             the input file was correctly sorted.

        1    Under the --cc option, the file was not ordered as specified, or if
             the --cc and --uu options were both specified, two input lines were
             found with equal keys.

       >1    An error occurred.

CCOONNSSEEQQUUEENNCCEESS OOFF EERRRROORRSS
       The default requirements shall apply, except that if _s_o_r_t encounters an
       error when opening or reading a _f_i_l_e operand, it may exit without
       writing any output to standard output or processing later operands.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The default value for --tt, <blank>, has different properties from, for
       example, --tt"<space>". If a line contains:


           <space><space>foo

       the following treatment would occur with default separation as opposed
       to specifically selecting a <space>:

                     +------+-------------------+--------------+
                     |FFiieelldd |      DDeeffaauulltt      | --tt ""<<ssppaaccee>>"" |
                     +------+-------------------+--------------+
                     |  1   | <space><space>foo | _e_m_p_t_y        |
                     |  2   | _e_m_p_t_y             | _e_m_p_t_y        |
                     |  3   | _e_m_p_t_y             | foo          |
                     +------+-------------------+--------------+
       The leading field separator itself is included in a field when --tt is
       not used. For example, this command returns an exit status of zero,
       meaning the input was already sorted:


           sort -c -k 2 <<eof
           y<tab>b
           x<space>a
           eof

       (assuming that a <tab> precedes the <space> in the current collating
       sequence). The field separator is not included in a field when it is
       explicitly set via --tt.  This is historical practice and allows usage
       such as:


           sort -t "|" -k 2n <<eof
           Atlanta|425022|Georgia
           Birmingham|284413|Alabama
           Columbia|100385|South Carolina
           eof

       where the second field can be correctly sorted numerically without
       regard to the non-numeric field separator.

       The wording in the OPTIONS section clarifies that the --bb, --dd, --ff, --ii,
       --nn, and --rr options have to come before the first sort key specified if
       they are intended to apply to all specified keys. The way it is
       described in this volume of POSIX.1-2017 matches historical practice,
       not historical documentation.  The results are unspecified if these
       options are specified after a --kk option.

       The --ff option might not work as expected in locales where there is not
       a one-to-one mapping between an uppercase and a lowercase letter.

       When using _s_o_r_t to process pathnames, it is recommended that LC_ALL, or
       at least LC_CTYPE and LC_COLLATE, are set to POSIX or C in the
       environment, since pathnames can contain byte sequences that do not
       form valid characters in some locales, in which case the utility's
       behavior would be undefined. In the POSIX locale each byte is a valid
       single-byte character, and therefore this problem is avoided.

       If the collating sequence of the current locale does not have a total
       ordering of all characters, this can affect the behavior of _s_o_r_t in the
       following ways:

        *  As _s_o_r_t --uu suppresses lines with duplicate keys, it suppresses
           lines that collate equally but are not identical.

        *  The output of _s_o_r_t (without --uu) can contain identical lines that
           are not adjacent, if it does not implement the recommended further
           byte-by-byte comparison of lines that collate equally. This affects
           the use of _s_o_r_t with _c_o_m_m and _u_n_i_q; see the APPLICATION USAGE for
           those utilities.

EEXXAAMMPPLLEESS
        1. The following command sorts the contents of iinnffiillee with the second
           field as the sort key:


               sort -k 2,2 infile

        2. The following command sorts, in reverse order, the contents of
           iinnffiillee11 and iinnffiillee22, placing the output in oouuttffiillee 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):


               sort -r -o outfile -k 2.2,2.2 infile1 infile2

        3. The following command sorts the contents of iinnffiillee11 and iinnffiillee22
           using the second non-<blank> of the second field as the sort key:


               sort -k 2.2b,2.2b infile1 infile2

        4. The following command prints the System V password file (user
           database) sorted by the numeric user ID (the third
           <colon>-separated field):


               sort -t : -k 3,3n /etc/passwd

        5. The following command prints the lines of the already sorted file
           iinnffiillee, suppressing all but one occurrence of lines having the same
           third field:


               sort -um -k 3.1,3.0 infile

RRAATTIIOONNAALLEE
       Examples in some historical documentation state that options --uumm with
       one input file keep the first in each set of lines with equal keys.
       This behavior was deemed to be an implementation artifact and was not
       standardized.

       The --zz option was omitted; it is not standard practice on most systems
       and is inconsistent with using _s_o_r_t to sort several files individually
       and then merge them together. The text concerning --zz in historical
       documentation appeared to require implementations to determine the
       proper buffer length during the sort phase of operation, but not during
       the merge.

       The --yy option was omitted because of non-portability. The --MM option,
       present in System V, was omitted because of non-portability in
       international usage.

       An undocumented --TT option exists in some implementations. It is used to
       specify a directory for intermediate files. Implementations are
       encouraged to support the use of the _T_M_P_D_I_R environment variable
       instead of adding an option to support this functionality.

       The --kk option was added to satisfy two objections. First, the zero-
       based counting used by _s_o_r_t is not consistent with other utility
       conventions. Second, it did not meet syntax guideline requirements.

       Historical documentation indicates that ``setting --nn implies --bb''.  The
       description of --nn already states that optional leading <blank>s are
       tolerated in doing the comparison. If --bb is enabled, rather than
       implied, by --nn, this has unusual side-effects. When a character offset
       is used in a column of numbers (for example, to sort modulo 100), that
       offset is measured relative to the most significant digit, not to the
       column.  Based upon a recommendation from the author of the original
       _s_o_r_t utility, the --bb implication has been omitted from this volume of
       POSIX.1-2017, and an application wishing to achieve the previously
       mentioned side-effects has to code the --bb flag explicitly.

       Earlier versions of this standard allowed the --oo option to appear after
       operands. Historical practice allowed all options to be interspersed
       with operands. This version of the standard allows implementations to
       accept options after operands but conforming applications should not
       use this form.

       Earlier versions of this standard also allowed the --_n_u_m_b_e_r and ++_n_u_m_b_e_r
       options. These options are no longer specified by POSIX.1-2008 but may
       be present in some implementations.

       Historical implementations produced a message on standard error when --cc
       was specified and disorder was detected, and when --cc and --uu were
       specified and a duplicate key was detected. An earlier version of this
       standard contained wording that did not make it clear that this message
       was allowed and some implementations removed this message to be sure
       that they conformed to the standard's requirements. Confronted with
       this difference in behavior, interactive users that wanted to be sure
       that they got visual feedback instead of just exit code 1 could have
       used a command like:


           sort -c file || echo disorder

       whether or not the _s_o_r_t utility provided a message in this case. But,
       it was not easy for a user to find where the disorder or duplicate key
       occurred on implementations that do not produce a message, especially
       when some parts of the input line were not part of the key and when one
       or more of the --bb, --dd, --ff, --ii, --nn, or --r options or _k_e_y_d_e_f type
       modifiers were in use. POSIX.1-2008 requires a message to be produced
       in this case. POSIX.1-2008 also contains the --CC option giving users the
       ability to choose either behavior.

       When a disorder or duplicate is found when the --cc option is specified,
       some implementations print a message containing the first line that is
       out of order or contains a duplicate key; others print a message
       specifying the line number of the offending line. This standard allows
       either type of message.

       Implementations are encouraged to perform the recommended further byte-
       by-byte comparison of lines that collate equally, even though this may
       affect efficiency. The impact on efficiency can be mitigated by only
       performing the additional comparison if the current locale's collating
       sequence does not have a total ordering of all characters (if the
       implementation provides a way to query this) or by only performing the
       additional comparison if the locale name associated with the LC_COLLATE
       category has an ''@@'' modifier in the name (since locales without an ''@@''
       modifier should have a total ordering of all characters -- see the Base
       Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _7_._3_._2, _L_C___C_O_L_L_A_T_E).  Note
       that if the implementation provides a _s_t_a_b_l_e _s_o_r_t option as an
       extension (usually --ss), the additional comparison should not be
       performed when this option has been specified.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       A future version of this standard may require that if the collating
       sequence of the current locale does not have a total ordering of all
       characters, any lines of input that collate equally when comparing them
       as whole lines are further compared byte-by-byte using the collating
       sequence for the POSIX locale.

SSEEEE AALLSSOO
       _c_o_m_m, _j_o_i_n, _u_n_i_q

       The Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _7_._3_._2, _L_C___C_O_L_L_A_T_E,
       _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t _V_a_r_i_a_b_l_e_s, _S_e_c_t_i_o_n _1_2_._2, _U_t_i_l_i_t_y _S_y_n_t_a_x
       _G_u_i_d_e_l_i_n_e_s

       The System Interfaces volume of POSIX.1-2017, _t_o_u_p_p_e_r()

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                              SORT(1)
