Portable way to achieve ls' -v flag (i.e. sort by version)?

被刻印的时光 ゝ 提交于 2019-12-04 01:34:32

问题


I'm working on a some build scripts that I'd like to depend on only standardized features. I need to sort some files by version. Say the files are bar-1.{0,2,3} bar-11.{0,2,3}.

By default, ls gives me:

bar-1_0
bar-11_0
bar-11_2
bar-11_3
bar-1_2
bar-1_3

Getting what I want is easy using 'ls -v':

bar-1_0
bar-1_2
bar-1_3
bar-11_0
bar-11_2
bar-11_3

The problem is that 'ls -v' is not standard. Standard sort also seems to lack the option I want, though I could be looking at old versions of the specs.

Can anyone suggest a portable way to achieve this effect short of writing my own sort routine?

Thanks, Rhys


回答1:


sort -n -t- -k2 seems to do what you want. -n gives you numeric (i.e. not alphabetic) sort; -t- sets the field separator to -, and -k2 selects the second field, i.e. the version number.

My sort, on Ubuntu, even does the part with the underscore correctly, but I'm not sure if that is standard. To be sure, you could sort by the minor version first, then by major version.



来源:https://stackoverflow.com/questions/1481824/portable-way-to-achieve-ls-v-flag-i-e-sort-by-version

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