Bash – Increment a variable using a Bash script

bashshell-scripttext processing

I have a file that contains the following text.

//
//  test_file.h
//  test project
//
//  Created by Test User
//

#ifndef test_file_constants_h
#define test_file_constants_h

// Generic includes
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define PF_MASTER_VERSION 3.0.0
#define PF_BUILD_VERSION 1

#endif

I need to write a script that can increment the PF_BUILD_VERSION by one each time it runs. I've tried looking at sed and AWK and failed!

Best Answer

An awk based solution could be:

awk '/^#define PF_BUILD_VERSION / {$3++} 1' infile >outfile  &&  mv outfile infile
Related Question