bash - Replace and increment tag value in XML file with sed -
i'm working in linux , in xml file i'd replace <version>
tag value string, when number value incremented 1 one. example if have xml file this:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.rm.core</groupid> <artifactid>rm-dt-agr</artifactid> <version>0.0.1-snapshot</version> <packaging>pom</packaging> <name>rm-dt-agr</name> </project>
i'd replace 0.0.1-snapshot
in tag <version>
0.0.2-snapshot
if it's 0.0.2-snapshot
value i'd replace 0.0.3-snapshot
, on...
i tried use "sed" command without success.
this 1 of situation should use tool meant processing xml rather trying shoehorn bash solution. being said, simple tag substitutions minimal format gymnastics involved, bash can work quite well.
for situation looking change line in file, need write changes new or temp file , copy replace original. (while theoretically, since changing single character , not adjusting line-length, can write changes new file, beware)
the difficulty here have 5 character sting, 3 digits separated by '.'
(periods) instead of simple number. no matter tool choose, have string manipulation parse down point know value need increment. once found, must preserve format (meaning must account time increment causes value change single-digit number two-digit number) presumably, must adjust value of next digit if increment causes single digit overflow.
there couple of approaches can take. can manually create new file reflects changes writing lines, except 1 changes, new file without modification. of course, write changed line well. second parse file line needs changing, calculation increment number , use sed
substitution.
the following takes manual approach , writes complete new file. on it, give try, , remember -- better served finding xml tool parse , modify xml...
#!/bin/bash [ -z $1 ] && { ## validate input file given printf "error: insufficient input. usage: %s filename\n" "${0//*\//}" exit 1 } [ -r $1 ] || { ## validate input file readable printf "error: file not found/readable\n" "$1" exit 1 } ifile="$1" ## set input/output filename, truncate output file ofile="${2:-newxml.xml}" :> "$ofile" while ifs= read -r line; ## each line text="${line// /}" ## remove leading spaces if [ "${text//>*/}" = '<version' ]; ## match version tag nspcs=$((${#line} - ${#text})) ## save # spaces removed value="${text#<version>}" ## strip tags ends value="${value%</version>}" nstr="${value%-*}" ## separate number string label="${value#*-}" ## text -snapshot ifs=. read v1 v2 v3 <<<"$nstr" ## read separate numbers [ -z $v1 -o -z $v2 -o -z $v3 ] && { ## validate printf "error invalid value: '%s'\n" "$value" continue } v3=$((v3 + 1)) ## increment 1 [ $v3 -eq 10 ] && { ## adjust remaining if 10 v3=0 v2=$((v2 + 1)) } [ $v2 -eq 10 ] && { v2=0 v1=$((v1 + 1)) } ## output updated line new file printf "%*s%s\n" $nspcs " " "<version>$v1.$v2.$v3-$label</version>" >> "$ofile" else printf "%s\n" "$line" >> "$ofile" ## write other lines unchanged fi done <"$ifile" exit 0
input file
$ cat dat/file.xml <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.rm.core</groupid> <artifactid>rm-dt-agr</artifactid> <version>0.0.1-snapshot</version> <packaging>pom</packaging> <name>rm-dt-agr</name> </project>
example use
$ bash xmlchgvalue.sh dat/file.xml dat/new.xml
output file
$ cat dat/new.xml <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.rm.core</groupid> <artifactid>rm-dt-agr</artifactid> <version>0.0.2-snapshot</version> <packaging>pom</packaging> <name>rm-dt-agr</name> </project>
verification
$ diff dat/file.xml dat/new.xml 8c8 < <version>0.0.1-snapshot</version> --- > <version>0.0.2-snapshot</version>
Comments
Post a Comment