linux bash'sort'按字典顺序排列 - 零问题

mic*_*ael 5 sorting bash

我在RedHat Enterprise Linux 5 x86_64和Ubuntu 9.1中看到了'sort'的奇怪之处.我正在使用bash.

首先,我认为使用字典顺序排序是正确的:

[stauffer @ unix-m sortTrouble] $ cat st1
1230
123
100
11
10
1
123
1230
100

[stauffer @ unix-m sortTrouble] $ sort st1
1
10
100
100
11
123
123
1230
1230

[stauffer @ unix -m sortTrouble] $

现在这里有第二列时出现的情况(制表符分隔,即使它看起来很混乱):

[stauffer @ unix-m sortTrouble] $ cat st2
1230 1
123 1
100 1
11 1
10 1
1 1
123 1
1230 1
100 1

[stauffer @ unix-m sortTrouble] $ sort st2
100 1
100 1
10 1
1 1
11 1
1230 1
1230 1
123 1
123 1

请注意第1列的排序顺序现在是如何不同的."11"在"1"之后正确放置,但"10"和"100"没有.同样的'1230'.似乎零会导致麻烦.

此行为不一致,并且在使用"join"时会导致问题,因为它需要字典排序.

在Mac OSX 10.5上,st2文件在第一列中排序为st1.

我错过了什么,或者这是一个错误?

谢谢,迈克尔

Any*_*orn 8

从手册页

   -b, --ignore-leading-blanks
          ignore leading blanks

   -g, --general-numeric-sort
          compare according to general numerical value

   -n, --numeric-sort
          compare according to string numerical value
Run Code Online (Sandbox Code Playgroud)

例如:

andrey@localhost:~/gamess$ echo -e "1\n2\n10" | sort
1
10
2
andrey@localhost:~/gamess$ echo -e "1\n2\n10" | sort -g
1
2
10
Run Code Online (Sandbox Code Playgroud)


Pau*_*ce. 4

通过将键限制为您感兴趣的列,可以按照您想要的方式执行排序:

sort -k1,1 inputfile
Run Code Online (Sandbox Code Playgroud)