如何模拟"排序-V"在Mac OSX [英] How to simulate "sort -V" on Mac OSX

查看:125
本文介绍了如何模拟"排序-V"在Mac OSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个bash脚本,我需要在依赖于sort命令Linux和Mac OSX相同的工作。我管道混帐标签的输出-l 来排序,以获得正确的语义顺序所有版本标签的列表。 GNU报价 -V 这使得这个AUTOMAGIC但所以我需要弄清楚如何没有它完成这个排序的Mac OSX并不支持这种说法。

  6.3.1.1
6.3.1.10
6.3.1.11
6.3.1.2
6.3.1.3
...

需要进行排序为

  6.3.1.1
6.3.1.2
6.3.1.3
...
6.3.1.10
6.3.1.11


解决方案

  SED的/ \\ B \\([0-9] \\)\\ B / 0 \\ 1 / G' versions.txt |排序| SED的/ \\ B0 \\([0-9] \\)/ \\ 1 / G'

要解释为什么这个作品,其本身考虑的第一个 SED 命令。有了您的输入作为versions.txt,第一个 SED 命令添加前导零到个位数的版本号,生产:

  06.03.01.01
06.03.01.02
06.03.01.03
06.03.01.10
06.03.01.11

上面可以正常分类。在这之后,它是去除添加的字符的问题。在完整的命令,最后 SED 命令删除前导零到产生最终的输出:

  6.3.1.1
6.3.1.2
6.3.1.3
6.3.1.10
6.3.1.11

作品只要版本号为99或更少。如果你超过99但小于1000具有版本号,该命令只得到稍微复杂一些:

  SED的/ \\ B \\([0-9] \\)\\ B / 00 \\ 1 /克; S / \\ B \\([0-9] [0-9] \\)\\ B / 0 \\ 1 / G'versions.txt |排序| SED的/ \\ B0 \\ + \\([0-9] \\)/ \\ 1 / G'

由于我没有在Mac,上面的方法在Linux上测试。

更新:在评论,乔纳森·莱弗勒说,尽管字边界( \\ b )是Mac的正则表达式文档,苹果 SED 似乎不承认它。他建议替换第一个 SED

  SED的/ ^ [0-9] \\ ./ 0安培; /; S / \\ \\([0-9] \\)$ / 0 \\ 1 /。 S / \\ \\([0-9] \\)\\ ./ 0 \\ 1./g。; S / \\。\\([0-9] \\)\\ ./。0 \\ 1./g

所以,完整的命令可能是:

  SED的/ ^ [0-9] \\ ./ 0安培; /; S / \\ \\([0-9] \\)$ / 0 \\ 1 /。 S / \\ \\([0-9] \\)\\ ./ 0 \\ 1./g。; S / \\ \\([0-9] \\)\\ ./ 0 \\ 1./g'versions.txt。|排序| SED的/ ^ 0 //; S / \\ 0 /./ G'

该处理的版本号到99。

I have written a bash script that I need to work identically on linux and mac osx that relies on the sort command. I am piping the output of git tag -l to sort to get a list of all the version tags in the correct semantic order. GNU offers -V which makes this automagic but mac osx does not support this argument so i need to figure out how to accomplish this sort order without it.

6.3.1.1
6.3.1.10
6.3.1.11
6.3.1.2
6.3.1.3
...

needs to be sorted as

6.3.1.1
6.3.1.2
6.3.1.3
...
6.3.1.10
6.3.1.11

解决方案

sed 's/\b\([0-9]\)\b/0\1/g' versions.txt  | sort | sed 's/\b0\([0-9]\)/\1/g'

To explain why this works, consider the first sed command by itself. With your input as versions.txt, the first sed command adds a leading zero onto single-digit version numbers, producing:

06.03.01.01
06.03.01.02
06.03.01.03
06.03.01.10
06.03.01.11

The above can be sorted normally. After that, it is a matter of removing the added characters. In the full command, the last sed command removes the leading zeros to produce the final output:

6.3.1.1
6.3.1.2
6.3.1.3
6.3.1.10
6.3.1.11

The works as long as version numbers are 99 or less. If you have version numbers over 99 but less than 1000, the command gets only slightly more complicated:

sed 's/\b\([0-9]\)\b/00\1/g ; s/\b\([0-9][0-9]\)\b/0\1/g' versions.txt  | sort | sed 's/\b0\+\([0-9]\)/\1/g'

As I don't have a Mac, the above were tested on Linux.

UPDATE: In the comments, Jonathan Leffler says that even though word boundary (\b) is in Mac regex docs, Mac sed doesn't seem to recognize it. He suggests replacing the first sed with:

sed 's/^[0-9]\./0&/; s/\.\([0-9]\)$/.0\1/; s/\.\([0-9]\)\./.0\1./g; s/\.\([0-9]\)\./.0\1./g'

So, the full command might be:

sed 's/^[0-9]\./0&/; s/\.\([0-9]\)$/.0\1/; s/\.\([0-9]\)\./.0\1./g; s/\.\([0-9]\)\./.0\1./g' versions.txt | sort | sed 's/^0// ; s/\.0/./g' 

This handles version numbers up to 99.

这篇关于如何模拟"排序-V"在Mac OSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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