SED 不使用复杂的正则表达式进行更新



作为构建过程的一部分,我正在尝试自动更新文件中的版本号。 我可以让以下内容工作,但仅适用于每个主要/次要/固定位置中个位数的版本号。

sed -i 's/version="[0-9].[0-9].[0-9]"/version="2.4.567"/g' projectConfig.xml

我尝试了一种更复杂的正则表达式模式,它可以在 MS 常规 Xpression 工具中工作,但在运行 sed 时不匹配。

sed -i 's/version="bd{1,3}.d{1,3}.d{1,3}b"/version="2.4.567"/g' projectConfig.xml

示例输入:

This is a file at version="2.1.245" and it consists of much more text.

期望的输出

This is a file at version="2.4.567" and it consists of much more text.

我觉得我缺少一些东西。

有 3 个问题:

要在sed中启用量词({}(,您需要-E/--regexp-extended开关(或使用{},请参阅 http://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html#Regular-Expressions(

字符集速记dsed[[:digit:]]

您的输入未引用"中的版本。

sed 's/version=b[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}b/version="2.4.567"/g' 
<<< "This is a file at version=2.1.245 and it consists of much more text."

为了保持便携性,您可能需要使用--posix开关(需要删除b(:

sed --posix 's/version=[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}/version="2.4.567"/g' 
<<< "This is a file at version=2.1.245 and it consists of much more text."

最新更新