Bash: 'sort' 如何对路径进行排序?

标签 bash shell sorting unix

排序 是如何工作的?我有这个文件:

/test# cat foobar
html/lib/ORM/aaa.php
html/lib/ORMBase/ormbase_aaa.php
html/lib/ORM/zzz.php
html/lib/ORMBase/ormbase_zzz.php

这是sort的输出:

/test# cat foobar | sort
html/lib/ORM/aaa.php
html/lib/ORMBase/ormbase_aaa.php
html/lib/ORMBase/ormbase_zzz.php
html/lib/ORM/zzz.php

我尝试了很多选项:-f-i-t/...但我不明白。我想了解为什么 sort 认为这是已排序的。


注意:它适用于其他示例:

/test# cat foobar2
a/a/a
a/ab/a
a/ab/b
a/a/ab
a/abc/a

/test# cat foobar2 | sort
a/a/a
a/a/ab
a/ab/a
a/ab/b
a/abc/a

最佳答案

sort 试图在本地化方面变得聪明。它会忽略一些非字母数字字符,如 / 等。手册页对此有一个简短的句子:

* 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.

因此,要解决您的问题:

$ cat foobar | LC_ALL=C sort

关于Bash: 'sort' 如何对路径进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938751/

相关文章:

Bash:如果目标在同一卷内则创建硬链接(hard link),否则复制

c++ - 在 C++ 的 unix shell 中也使用文本文件时,如何要求用户输入?

mysql - MYSQL 接受用户输入? Shell 脚本/批处理文件

arrays - Array.Sort(array, Comparison<t>) 如何执行?

database - BDB,如何按指定顺序获取主键?

linux - 如何将变量传递给命令行程序?

Java 错误 : Could not find the main class

bash - 如何在一个命令中 curl 多个资源?

bash - 如何将 curl 跟踪输出捕获到日志文件中

javascript - 如何按小时对数据进行排序?