Results 1 to 5 of 5
Hey guys,
I'm pulling a version number from a setup.py file which will be in the form of 'version=#.#...#' (i.e. version=0.1 or version=0.1.2, etc). I want to be able to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-23-2012 #1Just Joined!
- Join Date
- Oct 2012
- Posts
- 3
Incrementing Version Number with sed in Bash
Hey guys,
I'm pulling a version number from a setup.py file which will be in the form of 'version=#.#...#' (i.e. version=0.1 or version=0.1.2, etc). I want to be able to increment the last integer and write the new value back to the same spot in the setup.py file.
Right now, I'm pulling the version number out by running -
I could then increment the version and write it back into the file, but that seems to be inefficient - I think you can do in-line replacements with sed by calling -Code:VERSION=`grep 'version=' $SRC_DIR/setup.py | awk -F\' '{print $2}'`
However, I'm very unfamiliar with regex/sed usage. So,Code:sed -i -e 's/version=OLD_VAL/version=NEW_VAL/g' $SRC_DIR/setup.py
1. Is this sed replacement possible?
2. If so, what regex values would I need to use in place of 'version=OLD_VAL' and 'version=NEW_VAL'?
Thanks!
- 10-24-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,700
you can try something like:
the ".bak" after the "-i" will save the original file to setup.py.bak. if you want to use bash variables instead you can do something like:Code:sed -i.bak 's/version=1.1/version=1.2/' setup.py
where OLD_VER and NEW_VER have been defined as bash variables. the key is to use double-quotes so that the bash variables are expanded.Code:sed -i.bak "s/version=$OLD_VER/version=$NEW_VER/" setup.py
or did you mean you want sed to increment the variable internally, kind of like a sed one-liner?
- 10-25-2012 #3Just Joined!
- Join Date
- Oct 2012
- Posts
- 3
Hey Atreyu, I'm looking for a one-liner. I'd love to be able to do a one line in-line replacement of the version number.
Right now I have to pull the last character, increment it, and then write it back into the file:
However, this just seems too round-about. If possible, I'd like to execute this in just one in-line sed command for simplification and efficiency. Any ideas?Code:LAST_CHAR=${VERSION#${VERSION%?}} NEW_VERSION=`echo $VERSION | sed 's/[0-9]$/'"$((LAST_CHAR+1))"'/' sed -i -e "s/\(version=\).*/\1\'${NEW_VERSION}\',/" $SRC_DIR/setup.py
Thanks!
- 10-25-2012 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,700
I don't know of a way to do that in sed.
However, bear in mind that sed "in line" replacements are still creating a tmp file (which it then renames to the original input file), it is just obfuscating the fact. i.e., run a strace on a "sed -i" command, or compare inodes of the input file before and after an in-line replacement.
- 10-25-2012 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi.
See man shtool-version:
Best wishes ... cheers, drlCode:... DESCRIPTION This command displays and maintains version information in file. The version is always described with a triple <version,revision,level> and is represented by a string which always matches the regular expression ""[0-9]+\.[0-9]+[sabp.][0-9]+"". ...Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote

