Confused about the GNU `sort (1)` numeric subfield

I want to sort a space separated tableby a numeric value that was found in the second field. I can assume that the second 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 the parameters. On my system, I found the line:

sort -n -k2.5 table

for work.

My question is why?

According to the manual 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 does the table sort -n -k2.4not work and sort -n -k2.5do?

+3
source share
2 answers

Answer: The leading space is counted as part of the field , unless:

sort -b -n -k2.4 table

or curious:

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

.


... ...

, :

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

.

+1

, , info sort :

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.

, -k 2 POS2?

+1

Source: https://habr.com/ru/post/1747733/


All Articles