1
\$\begingroup\$

I have a specs file:

Name:           program
Version:        2.3.3
Release:        0
...

I want to update the version number every time it's built. I perform the build from a bash script so I'm calling sed to update the SPECS file and a "version" file which holds the version number.

I am using the following sed script to perform the update:

sed -ri 's/^(Version:\s+)([0-9.]+)/\1$version/g' SPECS/*.spec

with the $version being provided by the bash script.

It's designed to look for the correct line and replace the version portion while preserving whatever whitespace has been used. I use *.spec so that it will work with a spec file of any name.

I felt that a sed one-liner made the most sense given the requirement.

\$\endgroup\$
1
\$\begingroup\$

Variables don't interpolate inside single quotes. You need double quotes.

No need to capture the old version that we're throwing away. The /g is redundant since the pattern can never match more than once per line (being anchored to start-of-line).

What happens with Version: 0.9a? Version:1? Version:? Better to take the whole line, possibly excluding comments, and relax the space requirement.

sed -ri "s/^(Version:\s*)[^#]*/\1$version/" SPECS/*.spec

It's possible for the description or another section to contain the trigger text, and we probably don't want to change that. Use sed's start,stop addressing to limit replacement to the top block only, stopping at the first %whatever header:

sed -ri "1,/^\s*%/s/^(Version:\s*)[^#]*/\1$version/" SPECS/*.spec
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.