简体   繁体   English

用 Bash 脚本中的 sed 替换文件中的版本号

[英]Replace version number in file with sed in Bash script

In my project.pro file I have:在我的project.pro文件中,我有:

DEFINES += VERSION=\\\"1.13.1\\\"

I'd like to replace whatever the current version number is, with a new one in a Bash script:我想用 Bash 脚本中的新版本号替换当前版本号:

VERSION_MAJOR=1
VERSION_MINOR=14
VERSION_PATCH=1

sed -i "s/\([0-9]+.[0-9]+.[0-9]+\)/\1${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}/" project.pro

Why is that not working?为什么这不起作用?

So far I have managed to get either no matches at all or then some weird replace-only-the-last-number substitutions.到目前为止,我已经设法要么根本没有匹配项,要么进行了一些奇怪的仅替换最后一个数字的替换。

You may use this sed :你可以使用这个sed

sed -i.bak -E "s/[0-9]+\.[0-9]+\.[0-9]+/$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH/" project.pro

Few problems in your attempt:您尝试中的问题很少:

  • Without extended regex mode ( -E ), + cannot be used unescaped.如果没有扩展的正则表达式模式 ( -E ), +不能不转义地使用。
  • dot needs to be escaped in a regex点需要在正则表达式中转义
  • No need to use a capture group and back-reference \\1 .无需使用捕获组和反向引用\\1

PS: .bak is extension of backup file so that you can get original file, in case of a wrong substitution. PS: .bak是备份文件的扩展名,以便您可以在替换错误的情况下获取原始文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM