如何在OSX中执行`sort -V`? [英] How to do `sort -V` in OSX?
问题描述
我为Linux bash shell编写了一个脚本.
I wrote a script for a Linux bash shell.
一行包含一个文件名列表并对它们进行排序.列表如下所示:
One line takes a list of filenames and sorts them. The list looks like this:
char32.png char33.png [...] char127.png
它从32变为127.
此列表的ls
的默认排序是这样的
It goes from 32 to 127.
The default sorting of ls
of this list is like this
char100.png char101.png [...] char32.png char33.png [...] char99.png
幸运的是,有sort
,它具有方便的-V
开关,可以正确地对列表进行排序(如第一个示例一样).
Luckily, there is sort
, which has the handy -V
switch which sorts the list correctly (as in the first example).
现在,我必须将此脚本移植到OSX,并且OSX中的sort
缺少-V
开关.
Now, I have to port this script to OSX and sort
in OSX is lacking the -V
switch.
您对如何正确排序此列表有一个聪明的主意吗?
Do you have a clever idea of how to sort this list correctly?
推荐答案
它们都是以固定字符串(在您的示例中为char
)开头吗?如果是这样:
Do they all start with a fixed string (char
in your example)? If so:
sort -k1.5 -n
-k1.5
表示对从第5个字符(即第一个数字)开始的第一个键(示例中只有一个键)进行排序. -n
表示按数字排序.这也适用于Linux.
-k1.5
means to sort on the first key (there’s only one key in your example) starting from the 5th character, which will be the first digit. -n
means to sort numerically. This works on Linux too.
这篇关于如何在OSX中执行`sort -V`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!