Bash Scripting – Increment Build Number

macossedshell-scripttext processing

I am trying to increment a build number by 1 using command line.

Here is the content of my test file:

SOME_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 4;
SOME_SECOND_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 4;

The result I want to obtain is the following:

SOME_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 5;
SOME_SECOND_DUMMY_VALUE = -1;
CURRENT_PROJECT_VERSION = 5;

I am trying to use something like:

sed -i -E "s/CURRENT_PROJECT_VERSION = (\d+);/CURRENT_PROJECT_VERSION = \1~;/" test.txt

I am not experienced in bash scripting and I don't know how I can increment the number by one. (I am using MacOS but the sed command is almost the same as on Linux)

Best Answer

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;
Related Question