juzzlin
juzzlin

Reputation: 47865

Replace version number in file with sed in Bash script

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.

Upvotes: 0

Views: 1640

Answers (1)

anubhava
anubhava

Reputation: 784878

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.

Upvotes: 1

Related Questions