snackpie has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a file that can have a line something like this :
#define MYVERSION 11
So, in the file I need to find the line starting with :
#define MYVERSION
and replace 11 with 11+1 = 12. Any tips, snippets related to this would be helpful. Thanks.

Replies are listed 'Best First'.
Re: Replace text in file
by kcott (Archbishop) on Apr 17, 2014 at 07:00 UTC

    G'day snackpie,

    Welcome to the monastery.

    There should sufficient information in (or linked from) "Perl introduction for beginners" to do this.

    If you provide us with more information regarding what you're actually having difficulty with, we can no doubt provide more help. As it stands, without any code or other hints, I've no idea whether your issue is opening a file, reading a file, finding the line, doing the arithmetic, etc. The guidelines in "How do I post a question effectively?" exlpain what sort of information to provide such that we are better able to assist you.

    -- Ken

Re: Replace text in file
by GrandFather (Saint) on Apr 17, 2014 at 06:54 UTC

    So what did you try already and how did it fail? If you haven't tried anything, what is the first thing you are having trouble with?

    Perl is the programming world's equivalent of English
Re: Replace text in file
by Laurent_R (Canon) on Apr 17, 2014 at 07:45 UTC
    There are numerous ways to achieve what you want. For the substitution itself, the only thing that might be slightly beyond a pure beginner's knowledge, the first idea that comes to mind is to use the substitution operator, s///. You'll find detailed documentation on this in this chapter Regexp Quote Like Operators of the perlop document. Look in particular at the s/PATTERN/REPLACEMENT/msixpodualgcer sub-chapter and at the e option modifier.
Re: Replace text in file
by johngg (Canon) on Apr 17, 2014 at 12:04 UTC

    This node might give you a hint.

    Cheers,

    JohnGG

Re: Replace text in file
by Yaerox (Scribe) on Apr 17, 2014 at 08:18 UTC

    Maybe another possible way for you could be using perl native function substr() instead of using regular expressions. Laurent_R already explained those above ;-)

      Yes, that could be used, but the problem I see with substr here is that we cannot be sure whether the version number has one or two or several digits, so you need further processing for determining that. To me, a regex substitution is simpler in this context.

        While I probably wouldn't use substr for this, I don't really see the number of (before or after) digits as being a problem. Here's a (multi-line) one-liner to explain:

        $ perl -Mstrict -Mwarnings -E ' my $find = q{#define MYVERSION }; my $flen = length $find; my @strings = ( q{#define MYVERSION 11}, q{#define MYVERSION 9}, q{#define MYVERSION 99} ); for (@strings) { say; substr($_, $flen) = substr($_, $flen) + 1; say; } ' #define MYVERSION 11 #define MYVERSION 12 #define MYVERSION 9 #define MYVERSION 10 #define MYVERSION 99 #define MYVERSION 100

        Anyway, that's all fairly academic: I'm pretty sure I would have reached for something like s/^($find)(\d+)/$1.($2+1)/e in the first instance.

        -- Ken

        "we cannot be sure whether the version number has one or two or several digits"

        Totally agree with you on that point.

        Regards