I am looking to write a bash script for something slightly more complicated than the usual find/replace via sed. I have a book called bash Cookbook that I have been trying to glean some inspiration from but I am not getting very far.
Basically I am trying to write a script to update the version numbers in a bunch of maven pom.xml files automatically. Here is the general setup I am looking at:
<!-- TEMPLATE:BEGIN
<version>@@VERSION@@</version>
-->
<version>1.0.0</version>
<!-- TEMPLATE:END -->
After running the script (with the new version number 1.0.1) I’d like the file to read this instead:
<!-- TEMPLATE:BEGIN
<version>@@VERSION@@</version>
-->
<version>1.0.1</version>
<!-- TEMPLATE:END -->
So this would be in the actual release pom file, with 1.0.0 being the current version (and I am trying to replace it with 1.0.1 or something). Obviously the version number will be changing so there isn’t a good way to do a find/replace (since the thing you want to find is variable). I am hoping to be able to write a bash script which can
- replace @@VERSION@@ with the actual version number
- delete the current version line
- write the updated version line on the line before the TEMPLATE:END (while preserving the @@VERSION@@ in the file – possibly do this by writing template out to a temp file, doing replacement, then back in?)
I can sort of do some of this (writing out to a new file, doing replacement) using an ant script a la
<replace file="pom.xml">
<replacefilter
token="@@VERSION@@"
value="${version}"/>
</replace>
But I am not sure what the best ways to a.) delete the line with the old version or b.) tell it to copy the new line in the correct place are. Anyone know how to do this or have any advice?
Assuming the new version number is in a shell variable
$VERSION, then you should be able to use:Note that this ignores the template version line with
@@VERSION@@, but only matches a three-part version number that appears between the lines containing TEMPLATE:BEGIN and TEMPLATE:END, leaving everything else (including other lines containing a<version>...</version>element) alone.You can decide how to do file overwriting (maybe your version of
sedis from GNU and it does that automatically on request with the-ioption), etc. You might also be able to use more powerful regular expression notations that lead to more compact matches. However, that should work on most versions ofsedwithout change.