man(1) Manual page archive


     SORT(1)                                                   SORT(1)

     NAME
          sort - sort and/or merge files

     SYNOPSIS
          sort [ -cmusMbdfinrtx ] [ -o output ] [ option ... ] [ file
          ... ]

     DESCRIPTION
          Sort sorts lines of all the files together and writes the
          result on the standard output.  The name - means the stan-
          dard 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 white space (spaces and tabs) in field
               comparisons.

          -d   `Phone directory' order: only letters, digits and white
               space are significant in string comparisons.

          -f   Fold lower case letters onto upper case.

          -i   Ignore characters outside the ASCII range 040-0176 in
               string comparisons.

          -n   An initial numeric string, consisting of optional white
               space, optional sign, and a nonempty string of digits
               with optional decimal point, is sorted by value.

          -g   Numeric, like -n, with e-style exponents allowed.

          -M   Compare as month names.  The first three characters
               after optional white space are folded to lower case and
               compared.  Invalid fields compare low to `jan'.

          -r   Reverse the sense of comparisons.

          -tx  `Tab character' separating fields is x.

          -k pos1,pos2
               Restrict the sort key to a string beginning at pos1 and
               ending at pos2. Pos1 and pos2 each have the form m.n,
               optionally followed by one or more of the flags
               Mbdfginr; m counts fields from the beginning of the
               line and n counts characters from the beginning of the
               field.  If any flags are present they override all the

     SORT(1)                                                   SORT(1)

               global ordering options for this key.  If .n is missing
               from pos1, it is taken to be 1; if missing from pos2,
               it is taken to be the end of the field.  If pos2 is
               missing, it is taken to be end of line.

          Under option -tx fields are strings separated by x; other-
          wise fields are non-empty strings separated by white space.
          White space before a field is part of the field, except
          under option -b.  A b flag may be attached independently to
          pos1 and pos2.

          When there are multiple sort keys, later keys are compared
          only after all earlier keys compare equal.  Except under
          option -s, lines with all keys equal are ordered with all
          bytes significant.

          Single-letter options may be combined into a single string,
          such as -cnrt:.  The option combination -di and the combina-
          tion of -n with any of -diM are improper.  Posix argument
          conventions are supported.

          These option arguments are also understood:

          -c   Check that the single input file is sorted according to
               the ordering rules; give no output unless the file is
               out of sort.

          -m   Merge; the input files are already sorted.

          -u   Unique.  Keep only the first of two lines that compare
               equal on all keys.  Implies -s.

          -s   Stable sort.  When all keys compare equal, preserve
               input order.  Unaffected by -r.

          -o output
               Place output in a designated file instead of on the
               standard output.  This file may be the same as one of
               the inputs.  The option may appear among the file argu-
               ments, except after --.

          -T tempdir
               Put temporary files in tempdir rather than in (the
               default) /usr/tmp.

          -ymemory
               Suggests using the specified number of bytes of inter-
               nal store to tune performance; an unspecified memory
               size is taken to be huge.

          +pos1 -pos2
               Classical alternative to -k, with counting from 0

     SORT(1)                                                   SORT(1)

               instead of 1, and pos2 designating next-after-last
               instead of last character of the key.  A missing char-
               acter count in pos2 means 0, which in turn excludes any
               -t tab character from the end of the key.  Thus +1 -1.3
               means the same as -k 2,2.3 and +1r -3 means the same as
               -k 2r,3.

     EXAMPLES
          `sort -u -k1f -k1 list'
               Print in alphabetical order all the unique spellings in
               a list of words where capitalized words differ from
               uncapitalized.

          `sort -t: -k3n /etc/passwd'
               Print the password file (passwd(5)) sorted by userid
               (the third colon-separated field).

          `sort -umM dates'
               Print the first instance of each month in an already
               sorted file.

     FILES
     SEE ALSO
          comm(1), join(1), uniq(1), look(1)

     DIAGNOSTICS
          Sort comments and exits with non-zero status for various
          trouble conditions and for disorder discovered under option
          -c.

     BUGS
          The never-documented default pos1=0 for cases such as sort
          -1 has been abolished.
          Trouble (e.g. crash or file-system overflow) encountered
          while overwriting an input with -o is irrecoverable.