sortingcommand-linecommandgnugnu-sort

Confused about GNU `sort(1)` of a numerical sub field


I wish to sort a space separated table, with the numerical value that found on the 2nd field. I can assume that the 2nd field is always fooN but the length of N is unknown:

antiq. foo11 girls
colleaguing foo2 Leinsdorf
Cousy foo0 Montgomeryville
bowlegged foo1 pollack
Chevrier foo10 ill-conceived
candlebomb foo3 seventieths
autochthony foo101 re-enable
beneficiate foo100 osteometric

I read man sort(1) and played with all sort of options. On my system I found the line:

sort -n -k2.5 table

to work.

My question is why?

According to the man page:

-k, --key=POS1[,POS2]
   start a key at POS1, end it at POS 2 (origin 1) 
...
POS is F[.C][OPTS], where F is the field number and C the characterposition in the
field. OPTS is one or more single-letter ordering options, which override global
ordering options for that key. If no key is given, use the entire line as the key. 

So why sort -n -k2.4 table don't work and sort -n -k2.5 does?


Solution

  • The answer is: the leading space(s) are counted as part of the field, unless:

    sort -b -n -k2.4 table
    

    or curiously:

    LC_ALL=C sort -t" " -n -k2.4 table
    

    that also yields the correct result.


    ... and one more thing ...

    It seem that it is better to use:

    sort -b -n -k2.4,2 table
    

    and thus limit the sort to the end of the 2nd field.