Search code examples
regexbashsedgrepregexp-replace

Increment version number in ini-style file using bash


I would like to write a bash script to increment all [metadata] minor-version numbers contained in files named setup.cfg.

The contents of a setup.cfg could be:

[metadata]
...
version = 1.2.3
...

[<optional-fields>]
...
version=4.5.6
...

I would like to only increment 1.2.3 to 1.3.3. I want to make sure not to increment the version number of [<optional-fields>] and take into account variable/no spacing between version, =, and <metadata version number>.

(edit)

I am able to pull all instances of setup.cfg using

filepath = find | grep "setup.cfg"

but am uncertain how to isolate [metadata]'s version instance. To locate the version instance, I have used:

[[:space:]]*(?<=\[metadata\])*[[:space:]]*(:?version)[[:space:]]*(:?=)[[:space:]]*

To replace the old version number with the new one, I was planning on using

sed -i "s/$old_version/$new_version" $filepath


Solution

  • Using GNU sed

    $  sed -E "/[metadata]/,/[/{s/(\<version ?= ?[^.]*\.)([0-9]+)/echo \1\$((\\2+1))/e;}" setup.cfg
    [metadata]
    ...
    version = 1.3.3
    ...
    
    [<optional-fields>]
    ...
    version=4.5.6
    ...