Sorting data based on second column of a file

You can use the key option of the sort command, which takes a “field number”, so if you wanted the second column:

sort -k2 -n yourfile

-n--numeric-sort compare according to string numerical value

For example:

$ cat ages.txt 
Bob 12
Jane 48
Mark 3
Tashi 54

$ sort -k2 -n ages.txt 
Mark 3
Bob 12
Jane 48
Tashi 54

Leave a Comment