Sorting file based on a numeric column

Hi,

I use UBUNTU 12.04.

I have a file with this structure:

Name 2 1245787 A G 12 14 12 14 ....
Name 1 1245789 C T 13 12 12 12.....

I would like to sort my file based on the second column so to have this output for example:

Name 1 1245789 C T 13 12 12 12.....
Name 2 1245787 A G 12 14 12 14 ....

The second column goes from 1 to 22. I used sort -k2,2 file name but it doesn't sort my numbers in the second column but it gives 1 then 10, then 11 to 19, then 2 to 22.

How I should sort this, I imagine having numeric values in my second column is the problem.

Thank you very much.

have you tried sort -nk2,2 ?

yes, I have -k2 and what it gives me is sorting like this:
1 then 10 (without 2 to 9)
then 11 to 19.
then 2, 20, 21 and 22.
then 3 to 9.

There was a typo in my previous comment, you need to add option -n (for numeric sort)

Cheers :wink:

1 Like

Oh, Ok, I just got it. It works very well, thank you very much.