Skip to main content

With the following example data, both columns are numeric, but the second has different numbers of digits.

2 9

1 1000

1 50

3 0

2 9
1 1000
1 50
3 0

I want to sort based on both columns. Specifying them separately with the numeric flag, -n, produces the result I want.

sort -n -k1,1 -k2,2 num.data.txt

sort -n -k1,1 -k2,2 num.data.txt

gives

1 50

1 1000

2 9

3 0

1 50
1 1000
2 9
3 0

which is what I want.

However,

sort -n -k1,2 num.data.txt

sort -n -k1,2 num.data.txt

gives data that appear to be sorted alphabetically:

1 1000

1 50

2 9

3 0

1 1000
1 50
2 9
3 0

I know that sort -n -k1,2 num.data.txt is the same as sort -n num.data.txt (which gives the same result) when there are only two columns, but the data I'm actually working with has more columns.

Why is there this discrepancy between the two methods?

With the following example data, both columns are numeric, but the second has different numbers of digits.

2 9

1 1000

1 50

3 0

I want to sort based on both columns. Specifying them separately with the numeric flag, -n, produces the result I want.

sort -n -k1,1 -k2,2 num.data.txt

gives

1 50

1 1000

2 9

3 0

which is what I want.

However,

sort -n -k1,2 num.data.txt

gives data that appear to be sorted alphabetically:

1 1000

1 50

2 9

3 0

I know that sort -n -k1,2 num.data.txt is the same as sort -n num.data.txt (which gives the same result) when there are only two columns, but the data I'm actually working with has more columns.

Why is there this discrepancy between the two methods?

With the following example data, both columns are numeric, but the second has different numbers of digits.

2 9
1 1000
1 50
3 0

I want to sort based on both columns. Specifying them separately with the numeric flag, -n, produces the result I want.

sort -n -k1,1 -k2,2 num.data.txt

gives

1 50
1 1000
2 9
3 0

which is what I want.

However,

sort -n -k1,2 num.data.txt

gives data that appear to be sorted alphabetically:

1 1000
1 50
2 9
3 0

I know that sort -n -k1,2 num.data.txt is the same as sort -n num.data.txt (which gives the same result) when there are only two columns, but the data I'm actually working with has more columns.

Why is there this discrepancy between the two methods?

edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
Source Link
njc
  • 35
  • 3

Bash numeric sort gives different results when columns are selected simultaneously vs. together

With the following example data, both columns are numeric, but the second has different numbers of digits.

2 9

1 1000

1 50

3 0

I want to sort based on both columns. Specifying them separately with the numeric flag, -n, produces the result I want.

sort -n -k1,1 -k2,2 num.data.txt

gives

1 50

1 1000

2 9

3 0

which is what I want.

However,

sort -n -k1,2 num.data.txt

gives data that appear to be sorted alphabetically:

1 1000

1 50

2 9

3 0

I know that sort -n -k1,2 num.data.txt is the same as sort -n num.data.txt (which gives the same result) when there are only two columns, but the data I'm actually working with has more columns.

Why is there this discrepancy between the two methods?