Sort by second column numeric values

From googling and reading man pages I figured out this sorts the first column by numeric values.

sort -g -k 1,1

Why does the -n option not work? The man pages were a bit confusing.

And what if I want to sort the second column numerically? I haven't been able to figure that out. The file is separated by a comma if that matters.

Wouldn't you think that it would make more sense to post a representative portion of the input file you want sorted, and an example of the wanted output as sorted?

The standard way to perform an increasing numeric sort on the 2nd field in a comma separated file would be:

sort -t, -k2,2n file

And the -g option is an extension not mentioned by the standards.

As Aia said, if you don't show us a sample of the data you're trying to sort and the sorted output you're hoping to get from that sample, it is hard for us to guess at why sort -n isn't working for you. Especially since you haven't shown us a sort command using the -n option that isn't working for you.

Ok here it is not sorting properly. 14 and several other numbers should not be as low as they are.

$ sort -n -k 1,1 medium.txt
0,11
7,5
21,9
23,7
30,2
39,3
57,3
58,1
61,5
71,7
72,3
85,6
14,13
19,10
20,11
31,18
34,14
38,11
39,14
50,15
50,18
51,20
54,13
67,18
100,14

Hi, cokedude

According to you how do you think it should be ordered?

Did you try Don's suggestion: sort -t, -k2,2n medium.txt

Yes.

$ sort -t, -k2,2n medium.txt
58,1
30,2
39,3
57,3
72,3
61,5
7,5
85,6
23,7
71,7
21,9
19,10
0,11
20,11
38,11
14,13
54,13
100,14
34,14
39,14
50,15
31,18
50,18
67,18
51,20

The -g option worked. Any idea why the -n option didn't work?

$ sort -n -k 1,1 medium.txt
0,11
7,5
21,9
23,7
30,2
39,3
57,3
58,1
61,5
71,7
72,3
85,6
14,13
19,10
20,11
31,18
34,14
38,11
39,14
50,15
50,18
51,20
54,13
67,18
100,14
$ sort -g -k 1,1 medium.txt
0,11
7,5
14,13
19,10
20,11
21,9
23,7
30,2
31,18
34,14
38,11
39,14
39,3
50,15
50,18
51,20
54,13
57,3
58,1
61,5
67,18
71,7
72,3
85,6
100,14

I guess we clearly do not understand what you mean when you say:

I suggested using sort -t, -k2,2n medium.txt
which you say produced the output:

58,1
30,2
39,3
57,3
72,3
61,5
7,5
85,6
23,7
71,7
21,9
19,10
0,11
20,11
38,11
14,13
54,13
100,14
34,14
39,14
50,15
31,18
50,18
67,18
51,20

which seems to be exactly what you requested (the second column is sorted with increasing numeric values from 1 to 20). If you meant that you wanted to sort the file with the 2nd field as a primary key and the 1st field as the secondary key with both fields sorted numerically, that could be done with any of the commands:

sort -t, -n -k2,2 -k1,1 file
sort -t, -k2,2n -k1,1n file
sort -nt, -k2,2 -k1,1 file

Instead you claim that the output:

0,11
7,5
14,13
19,10
20,11
21,9
23,7
30,2
31,18
34,14
38,11
39,14
39,3
50,15
50,18
51,20
54,13
57,3
58,1
61,5
67,18
71,7
72,3
85,6
100,14

is the output you want even though it seems to be sorted numerically on the 1st column; not the 2nd column as you requested???

Did we misunderstand what you meant when you said you wanted to sort the 2nd column numerically?

Did we misunderstand what you meant when you said the comma was your column separator?

What locale settings are you using for LC_NUMERIC and LC_COLLATE?

What operating system are you using?

1 Like

You're not supplying the comma separator to sort . May I guess your locale allows for the comma being part of a number which would explain the difference between -n and -g options: