Bash Using sort Sort by keys


Example

Suppose we have this file:

test>>cat Hogwarts 
  Harry        Malfoy        Rowena        Helga
  Gryffindor   Slytherin     Ravenclaw     Hufflepuff
  Hermione     Goyle         Lockhart      Tonks
  Ron          Snape         Olivander     Newt
  Ron          Goyle         Flitwick      Sprout

To sort this file using a column as key use the k option:

test>>sort -k 2 Hogwarts

This will sort the file with column 2 as the key:

  Ron        Goyle        Flitwick        Sprout
  Hermione   Goyle        Lockhart        Tonks
  Harry      Malfoy       Rowena          Helga
  Gryffindor Slytherin    Ravenclaw       Hufflepuff
  Ron        Snape        Olivander       Newt

Now if we have to sort the file with a secondary key along with the primary key use:

sort -k 2,2 -k 1,1 Hogwarts 

This will first sort the file with column 2 as primary key, and then sort the file with column 1 as secondary key:

  Hermione        Goyle        Lockhart        Tonks
  Ron             Goyle        Flitwick        Sprout
  Harry           Malfoy       Rowena          Helga
  Gryffindor      Slytherin    Ravenclaw       Hufflepuff
  Ron             Snape        Olivander       Newt

If we need to sort a file with more than 1 key , then for every -k option we need to specify where the sort ends. So -k1,1 means start the sort at the first column and end sort at first column.

-t option

In the previous example the file had the default delimeter - tab. In case of sorting a file that has non-default delimeter we need the -t option to specify the delimeter. Suppose we have the file as below:

test>>cat file
5.|Gryffindor
4.|Hogwarts
2.|Harry
3.|Dumbledore
1.|The sorting hat

To sort this file as per the second column, use:

test>>sort -t "|" -k 2 file

This will sort the file as below:

3.|Dumbledore
5.|Gryffindor
2.|Harry
4.|Hogwarts
1.|The sorting hat