I want to replace the version number of multiple pom.xml files of a specific dependency.
As I understood:
sed is meant for replacing a string in one line and I have difficulties replacing the next line.
awk is intended for multiple lines. however I have difficulties replacing the text while using it with find. So far I came up with this solution but I don't know how to replace the string in the file? Is awk the right tool anyway for my purposes?
$ find . -name "pom.xml" -exec \
awk -v artifactId="junit" -v RS="<dependency>" '
$0 ~ "<artifactId>" artifactId "</artifactId>" {
sub("<version>.*</version>", "<version>NEW-VERSION</version>")
}
{ printf "%s", $0 RS }
' {} \;
Input are typical pom.xml files:
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>VARIOUS DIFFERENT VERSION NUMBERS HERE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
...
Expected Output:
...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>NEW-VERSION</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
...
I left Eds edit, since it made the query more readable and I leave the question despite Williams comment, that this is not the proper tool for version management.