给定带有混合文本和数字(不带前导零)的输入,我如何才能按“自然”顺序对其进行排序?例如,给定以下输入(主机名):
whatever-1.example.org
whatever-10.example.org
whatever-11.example.org
whatever-12.example.org
whatever-13.example.org
whatever-2.example.org
whatever-3.example.org
whatever-4.example.org
whatever-5.example.org
whatever-6.example.org
whatever-7.example.org
whatever-8.example.org
whatever-9.example.org
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:
whatever-1.example.org
whatever-2.example.org
whatever-3.example.org
whatever-4.example.org
whatever-5.example.org
whatever-6.example.org
whatever-7.example.org
whatever-8.example.org
whatever-9.example.org
whatever-10.example.org
whatever-11.example.org
whatever-12.example.org
whatever-13.example.org
Run Code Online (Sandbox Code Playgroud)
我应该提到,除了“随便”之外,还有
thingaroo-#.example.org
.
:
blargh-#.example.org
.
:
...etc...
Run Code Online (Sandbox Code Playgroud)
谢谢!
如果你有 GNU coreutils ?7.0,那么就可以使用版本排序了。这是字典顺序,除了数字序列根据它们作为十进制表示法的整数的值进行排序。
sort -V
Run Code Online (Sandbox Code Playgroud)