How To Sort Data In A File Using Linux

Linux Sort Command
Linux Sort Command.


In this guide, I will show you how to sort data in delimited files and from the output of other commands.

You won't be surprised to learn that the command you use to perform this task is called "sort". All of the major switches of the sort command will be provided in this article.

Sample Data

The data in a file can be sorted as long as it is delimited in some way.

For example, let's take the final league table from the Scottish Premier League last year and store the data in a file called "spl".

You can create a data file as follows with one club and the data for that club separated by commas on each row.

TeamGoals ScoredGoals AgainstPoints
St Johnstone585556
Ross County556148
Dundee United457028

How To Sort Data In Files

From that table, you can see that Celtic won the league and Dundee United came last. If you are a Dundee United fan you might want to make yourself feel better and you could do this by sorting on goals scored.

To do this run the following command:

sort -k2 -t, spl

This time the order would be as follows:

  • Partick
  • Kilmarnock
  • Hamilton
  • Dundee United
  • Motherwell
  • Dundee
  • Inverness
  • Ross County
  • St Johnstone
  • Hearts
  • Aberdeen
  • Celtic

The reason the results are in this order is that column 2 is the goals scored column and the sort goes from lowest to highest. 

The -k switch lets you choose the column to sort by and the -t switch lets you choose the delimiter.

To make themselves really happy the Dundee United fans can sort by column 4 using the following command:

sort -k4 -t, spl

Now Dundee United are top and Celtic are at the bottom.

Of course, this would make both Celtic and Dundee fans very unhappy indeed. To put things right you can sort in reverse order using the following switch:

sort -k4 -t, -r spl

A rather bizarre switch lets you sort randomly which really just jumbles up the rows of data.

You can do this using the following command:

sort -k4 -t, -R spl

This could cause real problems if you mixed up your -r and your -R switch.

The sort command can also sort dates into month order. To demonstrate look at the following table:

MonthData Used

The above table represents the month of the year and the amount of data used on a mobile device.

You can sort the dates alphabetically using the following command:

sort -k1 -t, datausedlist

You can also sort by month using the following command:

sort -k1 -t, -M datausedlist

Now obviously the table above already shows them in month order but if the list was randomly populated then this would be a simple way of sorting them.

Looking at the second column you can see that all of the values are in a human readable format which doesn't look like it would be easy to sort but the sort command can sort the data used column by using the following command:

sort -k2 -t, -h datausedlist


How To Sort Data Passed In From Other Commands

Whilst sorting data in files is useful, the sort command can also be used to sort the output from other commands:

For example look at the ls command:

ls -lt

The above command returns each file as a row of data with the following fields displayed in columns:

  • permissions
  • node count
  • username
  • groupname
  • size
  • last access date
  • file name

You can sort the list by file size by running the following command:

ls -lt | sort -k5

To get the results in reverse order you would use the following command:

ls -lt | sort -k5 -r

The sort command can also be used in conjunction with the ps command which lists processes running on your system.

For instance run the following ps command on your system:

ps -eF

The above command returns a lot of information about the processes currently running on your system.

One of those columns is the size and you might want to see which processes are the biggest.

To sort this data by size you would use the following command:

ps -eF | sort -k5


There isn't much to the sort command but it can become useful very quickly when sorting output from other commands into a meaningful order especially when the command doesn't have its own sort switches available.

For more information read the manual pages for the sort command.