Bash: How to Sort File by Multiple Columns


Often you may want to use Bash to sort a file based on values in multiple columns.

You can use the following basic syntax to do so:

sort -k1,1 -k2,2n player_data.txt

This particular example will sort the rows of the file named player_data.txt first based on the values in column 1, then by the values in column 2.

Here is what the various commands do:

  • sort: Specifies that we would like to sort the rows of the file
  • k: Stands for “key” and represents the column number to sort by. For example, k1,1 says to sort by column 1 to 1, i.e. only column 1.
  • n: Stands for “numeric sort” and specifies that the column should be interpreted as numbers and not text, otherwise a number like “11” would come before a number like “2”

The following examples shows how to use this syntax in practice.

Example: Sort by Column in Ascending Order

Suppose that we have a text file named player_data.txt that contains information about the team name and points scored by various basketball players:

Suppose that we would like to sort the rows of the file in the following manner:

  • First, by the values in column 1 in ascending order.
  • Then, by the values in column 2 in ascending order.

We can use the following syntax to do so:

sort -k1,1 -k2,2n player_data.txt

The following screenshot shows how to use this syntax in practice:

Bash sort by multiple columns

Notice that the rows are sorted by the first column in ascending order (A to Z) and then by the values in the second column in ascending order (smallest to largest).

Note that we could also use the r flag to sort one of the columns in “reverse” order, i.e. descending order.

For example, suppose that we would like to sort the rows in the following manner:

  • First, by the values in column 1 in ascending order.
  • Then, by the values in column 2 in descending order.

We can use the following syntax to do so:

sort -k1,1 -k2r,2n player_data.txt

The following screenshot shows how to use this syntax in practice:

Bash sort by multiple columns in reverse order

Notice that the rows are sorted by the first column in ascending order (A to Z) and then by the values in the second column in descending order (largest to smallest).

Note: In both examples we had to use the n flag to specify that column 2 should be interpreted as numbers instead of text.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Count Number of Unique Lines in File
Bash: How to Count Number of Characters in String
Bash: How to List All Files in Folder by Size

Leave a Reply