Confundido sobre GNU `sort (1)` de un campo sub numérica
-
05-10-2019 - |
Pregunta
I desea ordenar un espacio separado table
, con el valor numérico que se encuentra en el segundo campo. Puedo asumir que el segundo campo es siempre FOON pero la longitud de n es desconocido:
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
He leído man sort(1)
y jugó con todo tipo de opciones. En mi sistema me encontré con la línea:
sort -n -k2.5 table
para el trabajo.
Mi pregunta es ¿Por qué?
De acuerdo con la página del manual:
-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.
Entonces, ¿por qué no sort -n -k2.4
mesa de trabajo y sort -n -k2.5
hace?
Solución 2
La respuesta es: el espacio inicial (s) se cuentan como parte del campo , a menos que:
sort -b -n -k2.4 table
o curiosamente:
LC_ALL=C sort -t" " -n -k2.4 table
que también produce el resultado correcto.
... y una cosa más ...
Se parece que es mejor para su uso:
sort -b -n -k2.4,2 table
y por lo tanto limitar el tipo hasta el final del segundo campo.
Otros consejos
No sé si ayuda, pero información especie establece la siguiente:
-t tipo: -k -k 5.3,5.4 2,2n
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.
Tal vez trate de añadir un espacio entre -k
y 2
, o tratar de establecer POS2
?