• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL



sort Sorts lines of text.
sort [-cmus] [-t <separator>] [-o <output_file>] [-bdfiMnr]] [+<POS1>
[-<POS2>]] [-k <POS1> [,<POS2>]] [<file> ...]

sort sorts, merges, or compares all the lines from the given files, or the standard input if no files are given. A name of - means standard input. By default, sort writes the results to standard output.
sort has three modes of operation: sort (default), checking for sortedness, and merge. These options affect the mode of operation:
-c Checks whether given files are already sorted. If they are not all sorted, prints an error message and exits with a status of 1.
-m Merges the given files by sorting them as a group. Each input file should already be sorted. It always works to sort rather than merge. merge is an option because it is faster when it is set up properly.
If any key fields are specified, sort compares each pair of fields, in the order specified on the command line, according to associated ordering options, until a difference is found or no fields are left.
If any global options (Mbdfinr) are given, but no key fields are specified, sort compares lines according to global options.
If all keys compare equal, or if no ordering options were specified at all, sort compares lines byte-by-byte in machine collating sequence. The -s option disables the last resort comparison, producing a stable report.
The following options affect the ordering of the output lines. They may be specified globally or as part of a specific key field.
-b Ignores leading blanks when finding sort keys in each line.
-d Sorts in dictionary order; ignores all characters except letters, digits, and blanks.
-f Folds lowercase characters into the equivalent uppercase characters.
-i Ignores characters outside the ASCII range 040–0176 (inclusive).
-M Compares as months. The first three nonblank characters are folded into lowercase and sorted jan < feb < ... < dec. Invalid names compare low to valid names. Option implies -b.
-n Compares by arithmetic value an initial numeric string consisting of any amount of whitespace, an optional - sign, and zero or more digits. Option implies -b.
-r Reverses the result of the comparison so that lines of greater value appear earlier rather than later in the sort.
Other available options:  
-o <output_file> Writes to the specified <output_file> instead of to standard output. If <output_file> is one of the input files, sort writes to a temporary file before writing to the <output_file>.
-t <separator> Uses character <separator> as the field separator when finding the sort keys in each line. By default, fields are separated by the empty string between a non-whitespace character and a whitespace character.
-u For default case or -m option, outputs the first of a sequence of lines that compare equal. For -c option, checks that no pair of consecutive lines compare equal.
+<POS1> [-<POS2>] Specifies a field within each line to use as a sorting key. The field consists of the portion of the line starting with <POS1> and up to but not including <POS2>, or to the end of the line, if <POS2> is not specified. The fields and character positions are numbered starting with 0.
-k <POS1>[,<POS2>] Alternative syntax for specifying sorting keys. The fields and character positions are numbered starting with 1.
-s Disables the last resort comparison.
A position has the form f.c, where f is the number of the field to use, and c is the number of the first character from the beginning of the field (for +POS) or from the end of the previous field (-POS). The .c part of a position may be omitted, in which case it is taken to be the first character in the field. If the -b option has been given, the .c part of a field specification is counted from the first nonblank character of the field (for +POS) or from the first nonblank character following the previous field (-POS).
A +POS or -POS argument may also have any of the option letters Mbdfinr appended to it, in which case the global ordering options are not used for that particular field. The -b option may be independently attached to either or both of the +POS and -POS parts of a field specification, and if it is inherited from the global options, it will be attached to both. If a -n or -M option is used, thus implying a -b option, the -b option is taken to apply to both +POS and -POS parts of a key specification. Keys may span multiple fields.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint