sort [−m] [−o output] [−bdfinru] [−t char] [−k keydef]... [file...]
sort [−c|−C] [−bdfinru] [−t char] [−k keydef] [file]
- Sort lines of all the named files together and write the result to the specified output.
- Merge lines of all the named (presorted) files together and write the result to the specified output.
- Check that a single input file is correctly presorted.
- 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 −u, a duplicate key) is detected, a warning message shall be sent to standard error indicating where the disorder or duplicate key was found.
- Same as −c, except that a warning message shall not be sent to standard error if disorder or, with −u, a duplicate key is detected.
- Merge only; the input file shall be assumed to be already sorted.
- −o output
- 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.
- 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.
- Specify that only <blank> characters and alphanumeric characters, according to the current setting of LC_CTYPE, shall be significant in comparisons. The behavior is undefined for a sort key to which −i or −n also applies.
- Consider all lowercase characters that have uppercase equivalents, according to the current setting of LC_CTYPE, to be the uppercase equivalent for the purposes of comparison.
- Ignore all characters that are non-printable, according to the current setting of LC_CTYPE. The behavior is undefined for a sort key for which −n also applies.
- 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 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.
- Reverse the sense of comparisons.
- 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 −k option, it shall be applied to all −k options. Otherwise, the −b option can be attached independently to each −k field_start or field_end option-argument (see below).
- −t char
- Use char as the field separator character; char shall not be considered to be part of a field (although it can be included in a sort key). Each occurrence of char shall be significant (for example, < char><char> delimits an empty field). If −t 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.
- −k keydef
- The keydef argument is a restricted sort key field definition. The format of this definition is:
- A pathname of a file to be sorted, merged, or checked. If no file operands are specified, or if a file operand is '−', the standard input shall be used.
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1‐2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- If set to a non-empty string value, override the values of all the other internationalization variables.
- 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 −b, −d, −f, −i, and −n options.
- Determine the location of message catalogs for the processing of LC_MESSAGES.
- All input files were output successfully, or −c was specified and the input file was correctly sorted.
- 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.
- An error occurred.
sort −c −k 2 <<eof y<tab>b x<space>a eof
sort −t "|" −k 2n <<eof Atlanta|425022|Georgia Birmingham|284413|Alabama Columbia|100385|South Carolina eof
- The following command sorts the contents of infile with the second field as the sort key:
sort −k 2,2 infile
- The following command 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):
sort −r −o outfile −k 2.2,2.2 infile1 infile2
- The following command sorts the contents of infile1 and infile2 using the second non-<blank> of the second field as the sort key:
sort −k 2.2b,2.2b infile1 infile2
- 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
- The following command prints the lines of the already sorted file infile, suppressing all but one occurrence of lines having the same third field:
sort −um −k 3.1,3.0 infile
sort −c file || echo disorder