Find the answer to your Linux question:
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.
  1. #1
    Just 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 -
    Code:
    VERSION=`grep 'version=' $SRC_DIR/setup.py | awk -F\' '{print $2}'`
    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:
    sed -i -e 's/version=OLD_VAL/version=NEW_VAL/g' $SRC_DIR/setup.py
    However, I'm very unfamiliar with regex/sed usage. So,

    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!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by JLucci View Post
    Code:
    sed -i -e 's/version=OLD_VAL/version=NEW_VAL/g' $SRC_DIR/setup.py
    you can try something like:
    Code:
    sed -i.bak 's/version=1.1/version=1.2/' setup.py
    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=$OLD_VER/version=$NEW_VER/" 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.

    or did you mean you want sed to increment the variable internally, kind of like a sed one-liner?

  3. #3
    Just 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:
    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
    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?

    Thanks!

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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.

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.
    Quote Originally Posted by JLucci View Post
    ... 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 ...
    See man shtool-version:
    Code:
    ...
    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]+"".
    ...
    Best wishes ... cheers, drl
    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •