对数字子字段的GNU`sort(1)`感到困惑

时间:2010-05-30 09:03:37

标签: sorting command-line command gnu gnu-sort

我希望使用第二个字段中找到的数值对空格分隔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 个答案:

答案 0 :(得分:1)

我不知道它是否有帮助,但info sort说明了以下内容:

  

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.

也许尝试在-k2之间添加空格,或尝试设置POS2

答案 1 :(得分:1)

答案是:领先的空间被视为字段的一部分,除非:

sort -b -n -k2.4 table

或好奇地:

LC_ALL=C sort -t" " -n -k2.4 table

也会产生正确的结果。


......还有一件事......

似乎最好使用:

sort -b -n -k2.4,2 table

因此将排序限制在第二个字段的末尾。