Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time ...
  1. #1
    Just Joined! drwatson_droid's Avatar
    Join Date
    Jun 2011
    Location
    India
    Posts
    2

    Smile Bash shell script: Str(007) to int(7),increment it(8) & convert back t

    Hi,
    I have the following requirement.

    There will be following text/line in a file (eg: search-build.txt)
    PRODUCT_VERSION="V:01.002.007.Build1234"

    I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get the item.
    rexpression_sed='PRODUCT_VERSION=\"V:\([0-9]*\).\([0-9]*\).\([0-9]*\).Build\([0-9]*\)\"'
    var2=`grep -sn $rexpression_sed search-build.txt | sed -e 's/'$rexpression_sed'/\1\t\2\t\3\t\4/g'| cut -f 3`
    echo $var2
    Output
    007

    How to convert this to integer (7), add 1 to it ( and again get back in the same form (00 in the easiest way? I will be using sed -i on the file, with this new string that can be used with RE for replacing that part!
    Or
    Is there any better method than above.

    Thanks for any help.

  2. #2
    Just Joined! drwatson_droid's Avatar
    Join Date
    Jun 2011
    Location
    India
    Posts
    2
    Re: Bash shell script: Str(007) to int(7),increment it( & convert back to string(00
    thanks solved
    newval=0
    filename=search-build.txt
    rexpression_sed='PRODUCT_VERSION=\"V:\([0-9]*\).\([0-9]*\).\([0-9]*\).Build\([0-9]*\)\"'

    incrementnum=`grep -sn $rexpression_sed $filename | sed -e 's/'$rexpression_sed'/\1\t\2\t\3\t\4/g'| cut -f 3`
    echo "Current Incremental number is: " $incrementnum
    if [ $incrementnum ]
    then
    calcval=$(expr $incrementnum + 1)
    #echo $calcval
    if [ $calcval -le 9 ];
    then
    newval="00$calcval"
    elif [ $calcval -le 99 ];
    then
    newval="0$calcval"
    else
    newval="$calcval"
    fi
    echo "New Incremental number is: " $newval
    rexpression_rep_final='PRODUCT_VERSION="V:\1.\2.'$ newval'.Build\4"'
    #replace with new number
    sed -i 's/'$rexpression_sed'/'$rexpression_rep_final'/' $filename
    fi

Posting Permissions

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