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

Monks , I am re write the issue to make it easier to read : How can I match the following in a file
TEST () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8','$5',9,$2,$4,$3);" >> $OPTHREE } MONKS () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',2329,$3);" >> $OPTHREE }
and only change the nubmer 2329 to 9999 under the monks part. What I have so far is not working :
open ( INPUT , "../INPUT" ) or die " couldn't open \n"; open ( COPYFILE , ">../INPUT/copyfile" ) or die " couldn't open file + for input\n"; my $flag = 0; # set this to 1 when you see your market # set it to 0 when you see END while(<INPUT>) { if ( $flag == 1 ) { s/MONKS.+?(2329)/9999/; $flag = 0 if /^}/; } $flag = 1 if /MONKS/; print COPYFILE; } close (COPYFILE); close(INPUT);

Replies are listed 'Best First'.
Re: changing input file updated
by Roger (Parson) on Feb 06, 2004 at 22:43 UTC
    use strict; use warnings; my $text = do { local $/; <DATA> }; $text =~ s/(MONKS[\s\(\)]+{.*?,\s*?)2329(\s*?,[^}]*})/${1}9999${2}/sg; print "$text"; __DATA__ 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 } MONKS () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',2329,$3);" >> $OPTHREE }

    And the output -
    MONKS () { echo "insert into monks values ('$1',sysdate,null,null,null,'OP',null, +'$8',9999,$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',9999,$3);" >> $OPTHREE }

Re: changing input file updated
by CountZero (Bishop) on Feb 06, 2004 at 22:34 UTC
    What exactly is not working?

    Does $flag get set and cleared? How much is being read by each loop through <INPUT>? Does the replacement work?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law