Skip to main content
U.S. flag

An official website of the United States government

Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

The Insider's Guide to Accessing NLM Data

sort


As new Insider's Guide classes are no longer being offered, this site is not currently being updated. Please refer to NCBI's E-utilities documentation for more up-to-date information.


The 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

man sort

into your terminal to see the manual page for sort.

Input

Multiple lines of text, either piped in from a previous command, or in a file.

Output

The input lines of text, sorted based on the options specified.

Sorting by the entire line

By default, the sort command sorts lines of text alphabetically by the entire contents of the line

sort

You can sort in reverse-alphabetical order using the argument -r:

sort -r

You can sort numerically, instead of alphabetically, from smallest to largest, by using the argument -n:

sort -n

You can sort numerically from largest to smallest by combining -n and -r:

sort -n -r

Sorting by a portion of the line

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.

When using -k, you can provide a single field number (e.g. -k 2) or a range of field numbers (-k 2,3, -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.

By default, 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.

Sorting by multiple portions of the line

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.

Last Reviewed: July 30, 2021