I am using Sed to replace the major, minor and release number in version.

In my case:

version= (version=major.minor.release.buildnumber)

I don't want to change buildnumber in this version number. I only want to change the major, minor and release using sed command.



I want the output as version= (Build number should not change (last part in my version ))

I used below command

sed -i -r s/version=(.*)/version=11.21.2/g <filename>

it giving the output as


which is not my requirement.

3 Answers 3


a compatible sed command would be:

$ sed 's/version=[1-9]\+[0-9]*\.[0-9]\+\.[0-9]\+/version=11.21.2/' <<<'version='

or with:

sed 's/version=[1-9]\+[0-9]*\(\.[0-9]\+\)\{2\}/version=11.21.2/' <<<'version='

or with:

sed -E 's/version=[1-9]+[0-9]*(\.[0-9]+){2}/version=11.21.2/' <<<'version='

or even like below if a version major_number also could start with 0 like minor and release parts:

sed -E 's/version=([0-9]+\.){3}/version=11.21.2./' <<<'version='

We're having to make an assumption here, that the version string (a string, not a number) consist of four fields, delimited by a period. So, just trap the last one in the LHS of your regex.

I also recommend focusing on the line beginning version= first, and only touching the part after the = character. Put your regex in single quotes so the \ escapes don't get preemptively escaped by the shell. Also, you don't need to use ( and ) around your match, because you're not keeping it for later.

sed -i -r '/version=/s/=.*\.[0-9][0-9]*$/version=11.21.2/g' <filename>

In the above, I'm allowing for the last field to be multiple numeric characters. You might want to put arbitrary characters in there, in which case:

sed  -r '/version=/s/=.*\.([^.]*)$/=11.21.2.\1/g' <filename>

To take an arbitrary change as input, say:

sed  -r '/version=/s/=.*\.([^.]*)$/='${newver}'.\1/g' <filename>

How do you expect your command to know which part of the version string to replace? The .* will match the whole rest of the line, so everything gets replaced.

Now you can either define a pattern the matches just the three numbers you want to replace (that's what αғsнιη does in different ways in his answer) or preserve the buildnumber with a reference like here: s/=.*(\..*)/=11.21.2\1/. The \. matches a literal dot, so the last dot and everything after it can't get matched by the first .*. Thus, the buildnumber will match the (\..*); putting it inside the () makes it accessible in the replacement as \1.

I add this answer because it seems easier to me than the regular expressions in the αғsнιη answer. Maybe a matter of taste.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .