0

In my project.pro file I have:

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

I'd like to replace whatever the current version number is, with a new one in a Bash script:

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.

2
  • Why \\\ is needed here? Commented Jan 6, 2020 at 18:11
  • It's some Qt project file garbage so that the value gets correctly passed on to the compiler as a string. The compiler must get -DVERSION=\"1.13.1\". Commented Jan 6, 2020 at 18:12

1 Answer 1

1

You may use this 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.
  • dot needs to be escaped in a regex
  • No need to use a capture group and back-reference \1.

PS: .bak is extension of backup file so that you can get original file, in case of a wrong substitution.

Sign up to request clarification or add additional context in comments.

2 Comments

What is that .bak ? It doesn't matter if it's there or not.
Added a note about .bak in answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.