sort options



Last revision July 20, 2004

Various command line options control the sorting process.

-tx Specify a character x to be the delimiter character for splitting the line into fields. See discussion above.
-b Ignore leading blanks in sort fields. See discussion above.
-n This option says to consider a field as a decimal number and sort according to its value. For example, if a file with three lines containing only the numbers 10, 2, and 2.5 was sorted normally (as ASCII characters), the output line order would be 10, 2, and 2.5, because the lines would be compared character by character, and 1 sorts before 2. To sort these correctly as numeric values, use the -n option. Then the lines would sort in the order 2, 2.5, and 10.
-r This option reverses the order of the sort, e.g., from Z to A or from higher number to lower number.
-u This option removes "duplicate" lines, leaving only one instance of each "unique" line in the output. A line is considered to be a duplicate of another if its sort keys match. In a case where only certain fields are specified as sort keys (e.g., +3 -5), then only those fields have to match to consider the line to be a duplicate, and thus be removed.

Other options can be specified to ignore alphabetic case or non-alphnumeric characters. See the on-line manual entry.

Sorting options may apply globally, if specified before any of the sort keys, or to a single sort key, if typed immediately adjacent to the beginning field position. Examples:

      sort -r +3 -5 +7 -8
Here, the -r option applies globally, meaning that lines will be sorted according to reverse order of all specified keys. On pangea, this option only works for ASCII sort orders, not for sorting by numeric value. To sort in reverse numeric value, put the -r option with the -n option on the specific sort field, as shown in the next example.

      sort +1rn -3 +0 -1
Here, the -r and -n options apply only to the first sort key (fields 1 up to 3), not the second key (field 0). Lines are sorted according to the reverse numeric order of the first key, and then for ties, according to normal ASCII order of the second key.

Comments or Questions?