Increment build number in bash
awk -F '= ' '/CURRENT_PROJECT_VERSION/{$2=$2+1";"}1' OFS='= ' input > output
Tests
cat file
SOME_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 4;
SOME_SECOND_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 4;
awk -F '= ' '/CURRENT_PROJECT_VERSION/{$2=$2+1";"}1' OFS='= ' file
SOME_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 5;
SOME_SECOND_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 5;
Another answer, using only awk
. This assumes that you adhere to the key <space> = <space> value;
syntax throughout:
awk '$1 == "CURRENT_PROJECT_VERSION" {$3=($3+1)";"}1' testfile.txt
This increases the third field by 1 in all lines starting with CURRENT_PROJECT_NUMBER
but otherwise prints all lines "as is" (this is the meaning of the 1
behind the code block).
Note that I'm not sure if the "increment anything starting with a number" syntax is portable, so to be sure (and only slightly longer), we can remove the trailing semicolon from field 3 before incrementing:
awk '$1 == "CURRENT_PROJECT_VERSION" {sub(";","",$3); $3=($3+1)";"}1' testfile.txt
With sed and bash:
FILE="test.txt"
REGEX="\(CURRENT_PROJECT_VERSION *= *\)\([0-9]\+\);"
version=$(sed -ne "s/${REGEX}/\2/p" ${FILE} | head -1)
((version++))
sed -ie "s/${REGEX}/\1${version};/" ${FILE}