Confused about GNU `sort(1)` of a numerical sub field
-
05-10-2019 - |
문제
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?
해결책 2
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.
다른 팁
I don't know if it helps but info sort states the following:
sort -t : -k 2,2n -k 5.3,5.4
Note that if you had written `-k 2' instead of `-k 2,2' `sort' would have used all characters beginning in the second field and extending to the end of the line as the primary _numeric_ key. For the large majority of applications, treating keys spanning more than one field as numeric will not do what you expect.
Perhaps try adding a space between -k
and 2
, or try setting POS2
?