sorting - gnu 排序的意外结果

标签 sorting unix gnu-sort

当我尝试对以下文本文件“输入”进行排序时:

test1 3   
test3 2
test 4

使用命令

sort input

输出正是输入。这是输出

od -bc input

:

0000000 164 145 163 164 061 011 063 012 164 145 163 164 063 011 062 012
          t   e   s   t   1  \t   3  \n   t   e   s   t   3  \t   2  \n
0000020 164 145 163 164 011 064 012
          t   e   s   t  \t   4  \n
0000027

这只是一个制表符分隔的文件,有两列。当我这样做时

sort -k 2

输出更改为

test3 2
test1 3
test 4

这就是我所期望的。但如果我这样做

sort -k 1

输入方面没有任何变化,而我希望“test”在“test1”之前排序。最后,如果我这样做的话

cat input | cut -f 1 | sort

我明白了

test
test1
test3
正如预期的那样。这有合乎逻辑的解释吗?默认情况下 sort 到底应该做什么,例如:

sort -k 1

我的排序版本:

sort (GNU coreutils) 7.4

最佳答案

来自手册页:

* WARNING * The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

看来export LC_ALL=C一定有帮助

关于sorting - gnu 排序的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2691821/

相关文章:

sorting - 仅当第二列存在时,Unix 排序才会发出超出预期顺序的行

linux - 试图了解 linux 中的排序实用程序

python - 根据列表对字典的值进行排序

java - 使用具有多个条件的比较器对列表进行排序。先进先出

java - 使用带有链表的冒泡排序

java - Java 方法可以根据带有对象变量引用的参数对数组进行排序吗?

c 同步写入 stdout 和 stderr

unix - 为什么 sed 为波浪号报告 "Invalid command code"?

c - 如何让一个子进程等待它的兄弟?

linux - GNU 排序 - 用于比较的默认算法是什么?