How to do `sort -V` in OSX?

烂漫一生 提交于 2019-12-22 08:40:01

问题


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

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

Luckily, there is sort, which has the handy -V switch which sorts the list correctly (as in the first example).

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?


回答1:


Do they all start with a fixed string (char in your example)? If so:

sort -k1.5 -n

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



来源:https://stackoverflow.com/questions/4265284/how-to-do-sort-v-in-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!