实现ls的可移植方式-v 标志(即按版本排序)?

发布于 2024-08-05 16:46:21 字数 602 浏览 4 评论 0原文

我正在开发一些构建脚本,我只想依赖标准化功能。 我需要按版本对一些文件进行排序。假设文件是​​ bar-1.{0,2,3} bar-11.{0,2,3}。

默认情况下, ls 给我:

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

使用“ls -v”很容易获得我想要的东西:

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

问题是 'ls -v' 不是标准。标准 sort 似乎也缺少我想要的选项,尽管我可能是查看旧版本的规格。

除了编写我自己的排序例程之外,任何人都可以建议一种可移植的方法来实现这种效果吗?

谢谢, 里斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2024-08-12 16:46:21

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

我的 sort 在 Ubuntu 上,甚至正确地执行了带下划线的部分,但我不确定这是否是标准的。当然,您可以先按次要版本排序,然后再按主要版本排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文