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

Hello monks , I am reading the following DATA file wich contain:
LOSER () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',23,$3);" >> $OPTHREE } TEST () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8','$5',9,$2,$4,$3);" >> $OPTHREE # the following line should remain unchanged echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',2329,$3);" >> $OPTHREE } MONKS () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',2329,$3);" >> $OPTHREE }
I need to only change the nubmer 2329 to 9999 under the monks part and re write it to the same file so my Output should contain the MONKS part as follow:
MONKS () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',9999,$3);" >> $OPTHREE }
I am doing the following but not getting the result I want it just not chanigning the value , can someone help:
use strict; use warnings; my $text = do { local $/; <DATA> }; $text =~ s/(MONKS[\s\(\)]+{.*?,\s*?)2329(\s*?,[^}]*})/${1}9999${2}/sg; print "$text";
thanks

janitored by ybiC: Retitle from "exreg within an input file" for better searching

Replies are listed 'Best First'.
Re: regex within an input file
by Art_XIV (Hermit) on Feb 09, 2004 at 14:54 UTC

    Sometimes it's just a lot easier to use two regexes:

    #!perl use warnings; use strict; while (<DATA>) { if ($_ =~ /^MONKS/ .. /}/) { #Range/flip-flop operator s/,2329,/,9999,/; } print; } __DATA__ LOSER () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',23,$3);" >> $OPTHREE } MONKS () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null +'$8',2329,$3);" >> $OPTHREE } TEST () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8','$5',9,$2,$4,$3);" >> $OPTHREE # the following line should remain unchanged echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',2329,$3);" >> $OPTHREE }

    Check your docs for the range operator and its 'flip-flop' (scalar) context if the '/^MONKS/ .. /}/' gives you trouble.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: regex within an input file
by ysth (Canon) on Feb 09, 2004 at 14:50 UTC
    Kind monks, please note that this question differs from the previous changing input file updated in that it is asking how to rewrite DATA. (It also is actually using the solution suggested in the previous thread.)

    Update: since nomonk else has bitten, this is what I came up with (though I'm sure there's a better way):

    Before reading from DATA, save the position in the file. (The position isn't 0, because the program before the __DATA__ mark counts.)

    my $begin_data = tell(DATA);
    Then, after you've read in the whole file and done the substitution to the variable, open $0 (this is the name of the script perl is running; see perlvar) with read/write access and advance to the beginning of the __DATA__ section, and print out your new data:
    open my $source, "+<", $0 or die "Couldn't reopen source file: $!\n"; seek($source, $begin_data, 0) or die "Couldn't seek to beginning of DA +TA: $!\n"; print $begin_data $text;
    (see perldoc tell/perldoc seek for more info.)

    If the data you are replacing with is shorter than the original, you'll have to do more to make the file position after the print be the new end of the file, but it sounds as if that isn't an issue.

Re: regex within an input file
by Anonymous Monk on Feb 09, 2004 at 14:44 UTC
    When debugging regexes you just use re 'debug'; to see what the regex engine is doing.
Re: exreg within an input file
by John M. Dlugosz (Monsignor) on Feb 09, 2004 at 20:15 UTC
    It works fine for me when I make $text a quoted string and paste in your lines for the pattern match. So I suppose the problem is with the DATA.