来自gnu排序的意外结果 [英] unexpected result from gnu sort
问题描述
当我尝试对以下文本文件"input"进行排序时:
when I try to sort the following text file 'input':
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
这只是一个由制表符分隔的文件,其中包含两列.当我这样做
It's just a tab separated file with two columns. When I do
sort -k 2
输出更改为
test3 2
test1 3
test 4
这是我所期望的.但是如果我这样做
which is what I would expect. But if I do
sort -k 1
输入没有任何变化,而我希望'test'在'test1'之前排序.最后,如果我愿意
nothing changes with respect to the input, whereas I would expect 'test' to sort before 'test1'. Finally, if I do
cat input | cut -f 1 | sort
我知道
test
test1
test3
符合预期.有逻辑的解释吗?默认情况下,确切应该执行的操作是这样的:
as expected. Is there a logical explanation for this? What exactly is sort supposed to do by default, something like:
sort -k 1
?
我的排序版本:
sort (GNU coreutils) 7.4
推荐答案
从手册页开始:
*警告* 环境指定的语言环境会影响 种类 命令.设置LC_ALL = C以获取使用的传统排序顺序 本国的 字节值.
* 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.
因此,看来导出LC_ALL = C 必须提供帮助
So it seems export LC_ALL=C must help
这篇关于来自gnu排序的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!