实现ls -v标志(即按版本排序)的可移植方式? [英] Portable way to achieve ls' -v flag (i.e. sort by version)?

查看:73
本文介绍了实现ls -v标志(即按版本排序)的可移植方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些构建脚本,这些脚本只依赖于标准化功能. 我需要按版本对一些文件进行排序.假设文件为bar-1.{0,2,3} bar-11.{0,2,3}.

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

默认情况下,ls给我:

By default, ls gives me:

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

使用'ls -v'轻松获得我想要的东西:

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

问题在于"ls -v"不是标准 .标准 sort 似乎也缺少我想要的选项,尽管我可以查看规范的旧版本.

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?

谢谢, 里斯

推荐答案

sort -n -t- -k2似乎可以满足您的要求. -n为您提供数字(即非字母)排序; -t-将字段分隔符设置为-,然后-k2选择第二个字段,即版本号.

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.

在Ubuntu上,我的sort甚至正确地使用了下划线,但是我不确定这是否是标准的.可以肯定的是,您可以先按次要版本进行排序,然后再按次要版本进行排序.

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.

这篇关于实现ls -v标志(即按版本排序)的可移植方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆