加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 无法解释sort(1)行为

发布时间:2020-12-14 02:09:33 所属栏目:Linux 来源:网络整理
导读:当我看到ls以奇怪顺序列出的下列文件时,我一直很困惑: Star Wars Episode II - Attack of the Clones (2002) BDRip.mkvStar Wars Episode III - Revenge of the Sith (2005) BDRip.mkvStar Wars Episode I - The Phantom Menace (1999) BDRip.mkvStar Wars
当我看到ls以奇怪顺序列出的下列文件时,我一直很困惑:

Star Wars Episode II - Attack of the Clones (2002) BDRip.mkv
Star Wars Episode III - Revenge of the Sith (2005) BDRip.mkv
Star Wars Episode I - The Phantom Menace (1999) BDRip.mkv
Star Wars Episode IV - A New Hope (1977) BDRip.mkv
Star Wars Episode VI - Return of the Jedi (1983) BDRip.mkv
Star Wars Episode V - The Empire Strikes Back (1980) BDRip.mkv

从人的角度来看,“我”应该先行,然后“行进”,等等.

所以我用以下内容创建了文件:

$cat 1
Star Wars Episode II - Attack
Star Wars Episode III - Revenge
Star Wars Episode I - The
Star Wars Episode IV - A
Star Wars Episode VI - Return
Star Wars Episode V - The

如果我对它进行排序它会给我这个:

$sort 1
Star Wars Episode II - Attack
Star Wars Episode III - Revenge
Star Wars Episode I - The
Star Wars Episode IV - A
Star Wars Episode VI - Return
Star Wars Episode V - The

但是,如果我删除’ – ‘并且在排序正确之后的所有内容:

$cat 1
Star Wars Episode II 
Star Wars Episode III 
Star Wars Episode I 
Star Wars Episode IV 
Star Wars Episode VI 
Star Wars Episode V 

$sort 1
Star Wars Episode I 
Star Wars Episode II 
Star Wars Episode III 
Star Wars Episode IV 
Star Wars Episode V 
Star Wars Episode VI

所以,只要我在空格后添加任何符号,它就会开始为我排序不可预测:

$cat 1
Star Wars Episode II y
Star Wars Episode III x
Star Wars Episode I z
Star Wars Episode IV w
Star Wars Episode VI v
Star Wars Episode V u

$sort 1
Star Wars Episode III x
Star Wars Episode II y
Star Wars Episode IV w
Star Wars Episode I z
Star Wars Episode VI v
Star Wars Episode V u

有关此排序行为的任何提示吗?

更新:排序:使用’en_CA.UTF-8’排序规则

根据下面的评论更新#2是因为语言环境.

ls | LANG=C sort
Star Wars Episode I - The Phantom Menace (1999) BDRip.mkv
Star Wars Episode II - Attack of the Clones (2002) BDRip.mkv
Star Wars Episode III - Revenge of the Sith (2005) BDRip.mkv
Star Wars Episode IV - A New Hope (1977) BDRip.mkv
Star Wars Episode V - The Empire Strikes Back (1980) BDRip.mkv
Star Wars Episode VI - Return of the Jedi (1983) BDRip.mkv

为什么然后UTF8语言环境使它与众不同?
我检查了ru_RU.UTF8(错误排序)和ru_RU.KOI8-R(正确排序)

更新#3这是关于语言环境:http://www.gnu.org/software/coreutils/faq/#Sort-does-not-sort-in-normal-order_0021

解决方法

我想我找到了正确的解释:

Gnu coreutils FAQ: Sort does not sort in normal order

发现于:sort not sorting as expected (space and locale)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读