Confuso circa GNU `sort (1)` di un campo numerico sub
-
05-10-2019 - |
Domanda
vorrei ordinare uno spazio separato table
, con il valore numerico che ha trovato al 2 ° campo. Posso solo supporre che il secondo campo è sempre Foon ma la lunghezza di n è sconosciuta:
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
Ho letto man sort(1)
e giocato con tutti i tipi di opzioni. Sul mio sistema ho trovato la linea:
sort -n -k2.5 table
al lavoro.
La mia domanda è perché?
Secondo la pagina man:
-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.
Allora perché tavolo sort -n -k2.4
non funzionano e sort -n -k2.5
fa?
Soluzione 2
La risposta è: lo spazio iniziale (s) sono conteggiati come parte del campo , a meno che:
sort -b -n -k2.4 table
o con curiosità:
LC_ALL=C sort -t" " -n -k2.4 table
che produce anche il risultato corretto.
... e un'altra cosa ...
E sembra che sia meglio usare:
sort -b -n -k2.4,2 table
e quindi limitare il tipo alla fine del 2 ° campo.
Altri suggerimenti
Non so se aiuta, ma informazioni sorta afferma il seguenti:
sorta -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.
Forse provare ad aggiungere uno spazio tra -k
e 2
, o provare a impostare POS2
?