'Replace the first 2 lines containing a string with a specific line with a bash script
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 1:[1]
This might work for you (GNU sed):
sed -E '/<version>/{x;s/^/x/;/xx{2}/!{x;s/(<version>)[^<]*/\1NEW VERSION/;x};x}' file
Match <version> on any line,swap to the hold space, increment a counter and if that counter exceeds the required number of overall substitutions, continue with no further processing.
Otherwise, swap back to the pattern space and substitute the new version.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | potong |
