How to Sort Data in a File Using Linux

The 'sort' command offers many ways to arrange structured data

Use the sort command to re-order data in delimited files and from the output of other commands.

This utility is commonly installed by default on major Linux distributions.

Linux Sort Command

Sample Data

The data in a file sorts as long as it is delimited in some way.

For example, take the final league table from the Scottish Premier League for one year and store the data in a file called spl. Create a data file as follows with one club and the data for that club separated by commas on each row.

Team Goals Scored Goals Against Points
Celtic 93 31 86
Aberdeen 62 48 71
Hearts 59 40 65
St Johnstone 58 55 56
Motherwell 47 63 50
Ross County 55 61 48
Inverness 54 48 52
Dundee 53 57 48
Partick 41 50 46
Hamilton 42 63 43
Kilmarnock 41 64 36
Dundee United 45 70 28

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.

Run the following command:


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 happy, Dundee United fans can sort by column 4 using the following command:


Now Dundee United is at the top, and Celtic is at the bottom.

This would make both Celtic and Dundee fans unhappy. To put things right, sort in reverse order using the following switch:

so

A switch lets you sort randomly, which jumbles up the rows of data.

You can do this using the following command:

so

Case matters! You'll change the order of the sort if you mix up the -r and -R switches.

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

Month Data Used
January 4G
February 3000K
March 6000K
April 100M
May 5000M
June 200K
July 4000K
August 2500K
September 3000K
October 1000K
November 3G
December 2G

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

Sort the dates alphabetically using the following command:

sort -k1

Sort by month using the following command:

sort -k1 -t

Looking at the second column, you can see that all 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

How to Sort Data Passed From Other Commands

The sort command restructures the output from other commands. For example, look at the ls command:


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

Sort the list by file size by running the following command:


To get the results in reverse order, use the following command:

ls

The sort command works in conjunction with the ps command, which lists processes running on your system.

For example, run the following ps command on your system:


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, use the following command: