3

我希望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呢?

4

2 回答 2

1

我不知道它是否有帮助,但信息排序说明如下:

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

也许尝试在-kand之间添加一个空格2,或者尝试设置POS2

于 2010-05-30T09:34:41.430 回答
1

答案是:前导空格算作字段的一部分,除非:

sort -b -n -k2.4 table

或奇怪的是:

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

这也会产生正确的结果。


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

似乎最好使用:

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

从而将排序限制在第二个字段的末尾。

于 2010-05-30T09:58:55.437 回答