This documentation reflects EDirect version 8.60, released on 4/26/2018.
We strive to keep this documentation up-to-date with the latest release. If you are looking for documentation on a more recent version of EDirect, or to find out more about new EDirect releases, please see the Release Notes of NCBI's EDirect documentation.
sort command allows you to sort output in a number of ways.
sort is an incredibly powerful and flexible tool; only a small selection of its features are discussed here.
To see a full list of arguments, options, and features of
sort, see the
sort documentation page, or type
into your terminal to see the manual page for
Multiple lines of text, either piped in from a previous command, or in a file.
The input lines of text, sorted based on the options specified.
By default, the
sort command sorts lines of text alphabetically by the entire contents of the line
You can sort in reverse-alphabetical order using the argument
You can sort numerically, instead of alphabetically, from smallest to largest, by using the argument
You can sort numerically from largest to smallest by combining
sort -n -r
If you wish to sort by a portion of the line, instead of by the contents of the entire line, you can specify a field to use as the sort key with the argument
-k, you can provide a single field number (e.g.
-k 2) or a range of field numbers (
-k 4,4, etc.). If you provide a single field number, the
sort command will sort by a portion of the line starting with the field number specified and continuing through the end of the line. For example:
sort -k 2
will sort lines by the portion of the line starting with the beginning of field 2 and continuing to the end of the line, while
sort -k 2,2
will sort lines by field 2 alone, and
sort -k 2,3
will sort lines by the portion of the line starting with the beginning of field 2 and continuing to the end of field 3.
sort uses any blank space as a field delimiter. To specify a different separator, use the argument
-t. For example:
sort -t "|" -k 2,2
sorts by the second field, and interprets the pipe character (“|”) as the separator between fields, while
sort -t "\t" -k 3,3
sorts by the third field, and interprets the tab character (“\t”) as the separator between fields.
You can use multiple
-k arguments to specify a series of fields to sort by, in case of ties. For example:
sort -k 2,2 -k 1,1
sorts by the second field and resolves ties by sorting by the first field.
You can also mix sort methods (alphabetical and numeric) by using the option
n with the
-k argument. For example:
sort -k 4,4n -k 1,1 -k 3,3n
sorts by the fourth field numerically, then resolves ties by sorting by the first field alphabetically, resolving any remaining ties by sorting by the third field numerically.