0

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

Alex
  • 47
  • 5

2 Answers2

2

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
...
HatLess
  • 10,622
  • 5
  • 14
  • 32
1

I would suggest not to use bash sed or grep for this, as the format is a bit complex for these tools. A simple python3 program to do this would be like this:

import configparser

config = configparser.ConfigParser()
config.read('setup.cfg')
version = config['metadata']['version'].split('.')
version[-1] = str(int(version[-1]) + 1)
config['metadata']['version'] = '.'.join(version)
with open('setup.cfg', 'w') as configfile:
  config.write(configfile)

Another option is to use the crudini tool, which is available at least on ubuntu repositories:

#!/bin/bash

IFS=. read -ra arr <<< "$(crudini --get setup.cfg metadata version)"
((arr[2] ++)) 
crudini --set setup.cfg metadata version "$(IFS=.; echo "${arr[*]}")"

Credit to this answer for suggesting the crudini tool.

user000001
  • 32,226
  • 12
  • 81
  • 108