Reputation: 15
I would like to increment sub part of string version="01.00.00.041"
by 1 using sed
.
For example: version="01.00.00.041"
to version="01.00.00.042"
and it should be dynamic not static. I mean, the version number will be changed everytime when I build, so it should be an input to the sed
and last 1 digit should be incremented.
Edit: (collected from the comment)
I have tried like below:
sed -i "s/version.*/version=\"${print $ "."$ "."$ "."$ +1}}\">/g" descriptor.xml
Upvotes: 0
Views: 1000
Reputation: 133458
EDIT: Since OP added an information so adding code as per information.
awk -v version="version=\"01.00.00.42\"" 'BEGIN{FS=OFS=".";s1="\""} match($0,version) && ++count==2{$NF+=1;$NF=$NF s1} 1' Input_file
Here is the working solution Added by OP:
grep version descriptor.xml | tail -n 1 | awk -F= '{print $2}' | awk -F\" '{print $2}' increatedNum=$(printf "%03d" expr $(echo $OldVersion | cut -d'.' -f4) + 1) NewVersion=echo $OldVersion | cut -d'.' -f1-3.$increatedNum sed -i "s/$OldVersion/$NewVersion/g" descriptor.xml
Could you please try following.
awk 'BEGIN{FS=OFS=".";s1="\""} {$NF+=1;$NF=$NF s1} 1' Input_file
Output will be as follows.
version="01.00.00.42"
In case you want to save output into Input_file itself use following then.
awk 'BEGIN{FS=OFS=".";s1="\""} {$NF+=1;$NF=$NF s1} 1' Input_file > temp && mv temp Input_file
OR as per @tshiono's comment you could try following too.
awk 'BEGIN{FS=OFS=".";s1="\""} {$NF+=1;$NF=sprintf("%03d%c", $NF, s1)} 1' Input_file
Upvotes: 1
Reputation: 52344
With perl:
$ echo 'version="01.00.00.041"' | perl -pe 's/(\d+)(?=")/sprintf("%0*d", length($1), $1+1)/e'
version="01.00.00.042"
Increments the last set of digits before a quote, with 0 padding based on the length of the field instead of a hardcoded number.
Upvotes: 2