gnu `sort(1)` of of a surtについて混乱している
-
05-10-2019 - |
質問
分離されたスペースを並べ替えたいです table
, 、2番目のフィールドで見つかった数値を使用します。 2番目のフィールドは常にFOONであると仮定できますが、Nの長さは不明です。
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
私は読む man sort(1)
あらゆる種類のオプションでプレイしました。私のシステムで私は線を見つけました:
sort -n -k2.5 table
働くために。
私の質問はです なぜ?
Man 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.
なぜ sort -n -k2.4
テーブルは機能しません sort -n -k2.5
やりますか?
解決 2
答えは次のとおりです。 主要なスペースはフィールドの一部としてカウントされます, 、 そうでもなければ:
sort -b -n -k2.4 table
または不思議なことに:
LC_ALL=C sort -t" " -n -k2.4 table
また、正しい結果が得られます。
... 後もう一つ ...
使用する方が良いようです:
sort -b -n -k2.4,2 table
したがって、ソートを2番目のフィールドの終わりまで制限します。
他のヒント
それが役立つかどうかはわかりませんが 情報ソート 次のように述べています:
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
?
所属していません StackOverflow