How to Sort Based on the Third Column?

The “sort” is the built-in command line tool that applies sorting on the specified file or input. By default, it sorts the alphabet in ascending order, letters/symbols after the numeric digits, lowercase letters to uppercase letters. In addition, it can also perform sorting operations based on month, numbers, alphabets, and column numbers.

This guide explains how to sort based on the third column of the input/file.

How to Sort Based on the Third Column?

The working of the sort command relies on its basic syntax that is written below:


$ sort [options] filename.txt

The main component of the above syntax is “sort”, and the square brackets represent its supported sorting flags/arguments.

Execute the “help” command of the “sort” utility to get the list of sorting options:

$ sort --help

Scroll down the page for more options. Let’s see how these options can sort the data based on the third column.

Sample File:

A sample file named “File1.txt” is shown in the terminal using the “cat” command that contains three columns:

$ cat File1.txt

The third column of the File1.txt has numeric and string values. 

To perform the sort operation based on the column 3 values execute the “sort” command followed by the “-k” flag in the terminal:

$ sort -k 3 File1.txt

In the above command, the -k specifies the column number in which the sorting of the entire file depends i.e “3” in this case:

The “sort” command has successfully sorted the File1.txt on the basis of the 3rd column.

Sort By Numeric Values

The “sort” command line tool assists the users to sort the entire file based on any particular column by numerical values. For this purpose, it uses the “-n(numbers)” flag:

$ sort -nk 3 File4.txt

Sort in Reverse Order

The “sort” command offers the “-r” flag that is used to reverse the sorted output of a text file based on the third column:

$ sort -rk 3 File1.txt

The “File1.txt” sorted output based on the third column has been reversed.

How to Save Sorted Output Based on the Third Column?

The “sort” command does not save the sorted output in the original file. It just sorts the output and keeps the original file content remain same.

To save the arranged/sorted output use the “-o” flag that automatically creates a file if not exist and save the output in it:

$ sort -k 3 -o output File1.txt           #Save Sorted result in "output" file          
$ cat output                              #For reading the file

The “cat” command verifies that a new file “output” has been created and the sorted output based on the third column of File1.txt has saved into it.


In Linux, the “sort” command line utility is used with the “-k” flag to perform the sorting operation based on the third column. The sort command displays the arranged output in the terminal but does not save it in the original file. For saving the sorted output in the new file the “sort” command utilizes the “-o” flag.

This guide has provided a detailed view to sort based on the third column.