Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Incrementing Version Number with sed in Bash

    Hey guys,

    I'm pulling a version number from a 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 file.

    Right now, I'm pulling the version number out by running -
    VERSION=`grep 'version=' $SRC_DIR/ | 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 -
    sed -i -e 's/version=OLD_VAL/version=NEW_VAL/g' $SRC_DIR/
    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'?


  2. #2
    Quote Originally Posted by JLucci View Post
    sed -i -e 's/version=OLD_VAL/version=NEW_VAL/g' $SRC_DIR/
    you can try something like:
    sed -i.bak 's/version=1.1/version=1.2/'
    the ".bak" after the "-i" will save the original file to if you want to use bash variables instead you can do something like:

    sed -i.bak "s/version=$OLD_VER/version=$NEW_VER/"
    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
    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:
    NEW_VERSION=`echo $VERSION | sed 's/[0-9]$/'"$((LAST_CHAR+1))"'/'
    sed -i -e "s/\(version=\).*/\1\'${NEW_VERSION}\',/" $SRC_DIR/
    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?


  4. $spacer_open
  5. #4
    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
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    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:
           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