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


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!


An awk based solution could be:

awk '/^#define PF_BUILD_VERSION / {$3++} 1' infile >outfile  &&  mv outfile infile
| improve this answer | |

A Perl approach:

perl -pe 's/^#define PF_BUILD_VERSION \K(\d+)/$1+2/e' file > newfile

Or, to edit the file in place:

perl -i -pe 's/^#define PF_BUILD_VERSION \K(\d+)/$1+2/e' file
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.