转换动态字母数字字符串 [英] Transform a dynamic alphanumeric string
本文介绍了转换动态字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 700-I20190808-0201
的内部版本.我需要将其转换为 7.0.0-I20190808-0201
.我可以使用正则表达式来做到这一点:
I have a Build called 700-I20190808-0201
. I need to convert it to 7.0.0-I20190808-0201
. I can do that with regular expression:
sed 's/\([0-9]\)\([0-9]\)\([0-9]\)\(.\)/\1.\2.\3\4/' abc.txt
但是,当内部版本ID为7001-I20190809-0201时,该解决方案将不起作用.我们可以使正则表达式动态化,使其同时适用于700和7001吗?
But the solution does not work when the build ID is 7001-I20190809-0201. Can we make the regular expression dynamic so that it works for both (700 and 7001)?
推荐答案
您可以使用标签和sed分支实现简单的循环:
You can implement a simple loop using labels and branching using sed:
$ echo '7001-I20190809-0201' | sed ':1; s/^\([0-9]\{1,\}\)\([0-9][-.]\)/\1.\2/; t1'
7.0.0.1-I20190809-0201
$ echo '700-I20190809-0201' | sed ':1; s/^\([0-9]\{1,\}\)\([0-9][-.]\)/\1.\2/; t1'
7.0.0-I20190809-0201
如果您的sed支持 -E
标志:
If your sed support -E
flag:
sed -E ':1; s/^([0-9]+)([0-9][-.])/\1.\2/; t1'
这篇关于转换动态字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文