Confus sur GNU `trier (1)` d'un champ de sous-numérique
-
05-10-2019 - |
Question
Je souhaite trier un espace séparé table
, avec la valeur numérique que l'on trouve sur le 2ème champ. Je suppose que le 2ème champ est toujours Foon mais la longueur de N est inconnu:
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
Je lis man sort(1)
et joué avec toutes sortes d'options. Sur mon système, je trouve la ligne:
sort -n -k2.5 table
au travail.
Ma question est pourquoi?
Selon la page de manuel:
-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.
Alors pourquoi table sort -n -k2.4
ne fait pas le travail et sort -n -k2.5
?
La solution 2
La réponse est: l'espace de tête (s) sont comptés dans le cadre du champ , à moins que:
sort -b -n -k2.4 table
ou curieusement:
LC_ALL=C sort -t" " -n -k2.4 table
qui donne également le résultat correct.
... et une chose ...
Il semble qu'il est préférable d'utiliser:
sort -b -n -k2.4,2 table
et ainsi limiter le genre à la fin du 2ème champ.
Autres conseils
Je ne sais pas si ça aide, mais info indique la suivant:
-t trier: -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.
Peut-être essayer d'ajouter un espace entre -k
et 2
, ou essayez de définir POS2
?