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

时间:2010-06-24 04:51:39

标签: bash sorting

我在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。

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

谢谢, 迈克尔

2 个答案:

答案 0 :(得分: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

例如:

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

答案 1 :(得分:4)

通过将密钥限制为您感兴趣的列,可以按照您希望的方式执行排序:

sort -k1,1 inputfile
相关问题