Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 5 down vote accepted

An awk based solution could be:

awk '/^#define PF_BUILD_VERSION / {$3++} 1' infile >outfile  &&  mv outfile infile
share|improve this answer
    
Perfect thank you –  Lee Armstrong 2 days ago

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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