John McDonnell
John McDonnell

Reputation: 803

Replacing XML content with Regular Expressions and SED

I have a project that's effectively 2 maven projects. In the "child" maven project I have a property that references the version in the parent project:

<myparent.project.version>15.0.0-SNAPSHOT</myparent.project.version>

What I'm trying to do is bash script a few common commands together, i.e. when creating a release branch I want to create one of the parent maven project, then update the child maven parent project but Im missing how to update this value:

I'm hoping to get something along the lines of working: where newVersion = 16.0.0

$ sed -i "s/\<myparent.project.version\>[0-9.]+-SNAPSHOT\<\/myparent.project.version\>/<myparent.project.version>$newVersion-SNAPSHOT<\/myparent.project.version>/g" pom.xml

I have checked the regex (\<myparent.project.version\>[0-9.]+-SNAPSHOT\<\/myparent.project.version\>) with https://regex101.com/ and it matches the line I want to replace in my pom.xml file, but for some reason, I can't get the sed to work correctly.

Any ideas?

Upvotes: 0

Views: 785

Answers (1)

SLePort
SLePort

Reputation: 15461

You should use an xml parser like XMLStarlet. That said, for that simple substitution, you can try this GNU sed:

sed "s/<myparent\.project\.version>[0-9.]\+-SNAPSHOT<\/myparent\.project\.version>/<myparent.project.version>$newVersion-SNAPSHOT<\/myparent.project.version>/g" file

You must escape the one or more quantifier: \+ and the dot: \. but not the < nor >(\< and \> are used for word boundaries with GNU sed)

Upvotes: 1

Related Questions