2

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?

    1 Answer 1

    5

    A -k1,2 key specification specifies one key that starts at the start of the first column (includes the leading blanks as the default column separator is the transition from a non-blank to a blank) and ends at the end of the second column.

    It's important to realise it's only one key. If you need two keys, you need two -k options. When sorting, sort will compare the "1 50" string with "1 1000" numerically. For a numerical comparison, those strings are converted to numbers by considering the leading part (ignoring leading blanks) that looks like a valid number. So we'll be comparing 1 and 1. As they are equal, sort will revert to the fall-back sorting to determine ties which is a lexical comparison of the whole line.

    With -n -k1,1 -k2,2, sort compares "1" with "1" and then as it's a tie, considers the second key (" 50" vs " 1000"). As it's a numerical sort, -n -k1 -k2 would also work (where -k1 specifies a key that starts at the first field and ends at the end of the line, same as the full line).

    2
    • That was an incredibly clear explanation that completely answers my question. Thank you so much!
      – njc
      CommentedMar 27, 2017 at 15:58
    • 3
      in GNU sort, add the --debug line for a visual representation (as well as an informative "sort: key 1 is numeric and spans multiple fields"
      – Jeff Schaller
      CommentedMar 27, 2017 at 16:02

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.