对数值子字段的gnu“排序(1)”感到困惑
-
05-10-2019 - |
题
我想对一个分开的空间进行分类 table
, ,带有第二个字段上发现的数值。我可以假设第二个字段始终是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
去工作。
我的问题是 为什么?
根据人页面:
-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
因此将排序限制在第二场的末尾。
其他提示
我不知道它是否有帮助,但是 信息排序 指出以下内容:
排序-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