Frage

Ich möchte ein Leerzeichen getrennt table sortieren, mit dem numerischen Wert, der auf dem zweiten Feld gefunden. Ich kann davon ausgehen, dass das zweite Feld ist immer foon aber die Länge von N ist unbekannt:

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

Ich las man sort(1) und mit aller Art von Optionen gespielt. Auf meinem System habe ich die Zeile:

sort -n -k2.5 table

zu arbeiten.

Meine Frage ist Warum?

Nach der man-Seite:

-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. 

Also, warum sort -n -k2.4 Tabelle nicht Arbeit und sort -n -k2.5 tut?

War es hilfreich?

Lösung 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.

Andere Tipps

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?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top