Issue
I need to replace the first 2 occurrences of:
"<version>*"
with
"<version>$NEW_VERSION</version>"
within an already existing xml file.
Original:
<groupId>my.test.group</groupId>
<artifactId>maven-parent</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
Desired result:
<groupId>my.test.group</groupId>
<artifactId>maven-parent</artifactId>
<version>1.3.1-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
I've tried for a while now and this:
sed -i -e '0,/<version>*/s/<version>*/<version>1.3.1-SNAPSHOT<\/version>/' pom.xml
gets close, but the resulting string is:
<groupId>my.test.group</groupId>
<artifactId>maven-parent</artifactId>
<version>1.3.1-SNAPSHOT</version>1.3.0-SNAPSHOT</version>
Solution
With awk:
$ awk -v new_version="1.3.1-SNAPSHOT" 'BEGIN {matches = 0} /^<version>/ && matches < 2 {print "<version>" new_version "</version>"; matches++; next} 1' pom.xml
<groupId>my.test.group</groupId>
<artifactId>maven-parent</artifactId>
<version>1.3.1-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
If you have whitespaces before <version>
lines like that:
<groupId>my.test.group</groupId>
<artifactId>maven-parent</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
and you want to save them:
$ awk -v new_version="1.3.1-SNAPSHOT" 'BEGIN {matches = 0} /^( *)<version>/ && matches < 2 { match($0, "^ *"); print substr($0, RSTART, RLENGTH) "<version>" new_version "</version>"; matches++; next} 1' pom.xml
<groupId>my.test.group</groupId>
<artifactId>maven-parent</artifactId>
<version>1.3.1-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
Answered By - Arkadiusz Drabczyk Answer Checked By - Pedro (WPSolving Volunteer)