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

Looking for assistance on how to loop through a list of files, search for a Multiline pattern and Replace the multiline. Below is all I have gotten so far but cant figure out how to match the multiline pattern. Any assistance is appreciated.

my $MODFILE; my @files = <Test*_Copy>; foreach $MODFILE (@files) { # Need to Search and Replace Here #How to Search entire contents of file and Replace??? s/Marry Had A\nLittle Lamb\nShe Was GOOD\n/CHANGED!!!\n/smg; } File Contents (Test1_Copy) Marry Had A Little Lamb She Was GOOD Not Changing this Or This Marry Had A Little Lamb She Was GOOD Line 9 Line 10

Replies are listed 'Best First'.
Re: Multiline search and replace in file(s)
by haukex (Archbishop) on Aug 09, 2020 at 08:03 UTC

    You can use the following pattern to slurp and spew a file:

    use warnings; use strict; my @files = <Test*_Copy>; for my $modfile (@files) { # read open my $ifh, '<', $modfile or die "$modfile: $!"; my $data = do { local $/; <$ifh> }; # slurp close $ifh; # modify $data =~ s/Marry Had A\nLittle Lamb\nShe Was GOOD\n/CHANGED!!!\n/s +mg; # write open my $ofh, '>', $modfile or die "$modfile: $!"; print $ofh $data; close $ofh; }

    (Slurping can also be shortened to my $data = do { open my $fh, '<', $modfile or die $!; local $/; <$fh> };, though that's a bit less readable if you're not used to it).

    Or you can use a nice module like Path::Class or Path::Tiny:

    use warnings; use strict; use Path::Tiny; my @files = path(".")->children(qr/^Test.*_Copy$/); for my $modfile (@files) { $modfile->edit(sub { s/Marry Had A\nLittle Lamb\nShe Was GOOD\n/CHANGED!!!\n/smg; }); }

    Or you can use a oneliner (see also $^I):

    perl -wMstrict -0777 -i -pe 's/Marry Had A\nLittle Lamb\nShe Was GOOD\ +n/CHANGED!!!\n/smg' Test*_Copy

    Or you can use my module File::Replace for atomic updates when the filesystem supports it:

    use warnings; use strict; use File::Replace 'replace3'; my @files = <Test*_Copy>; for my $modfile (@files) { my ($infh,$outfh,$repl) = replace3($modfile); my $data = do { local $/; <$infh> }; $data =~ s/Marry Had A\nLittle Lamb\nShe Was GOOD\n/CHANGED!!!\n/s +mg; print $outfh $data; $repl->finish; }

    Or you can open the file in read-write mode, truncate it after reading, and then write it - though note I see this being done very rarely. (It's useful if you want to flock the file while editing. That can be as simple as adding use Fcntl qw/:flock/; at the top of the code and flock($fh, LOCK_EX) or die "flock $modfile: $!"; immediately after the open. Keep in mind that flock locks are merely advisory, though!)

    use warnings; use strict; my @files = <Test*_Copy>; for my $modfile (@files) { open my $fh, '+<', $modfile or die "$modfile: $!"; my $data = do { local $/; <$fh> }; $data =~ s/Marry Had A\nLittle Lamb\nShe Was GOOD\n/CHANGED!!!\n/s +mg; seek $fh, 0, 0 or die $!; truncate $fh, 0 or die $!; print $fh $data; close $fh; }

    Note that glob has several caveats, though as shown in this code it's ok.

    Updates: Added info on flock and $^I.

Re: Multiline search and replace in file(s)
by AnomalousMonk (Archbishop) on Aug 09, 2020 at 08:18 UTC

    Note that this will deal with Mary and her little lamb over any line boundaries:

    c:\@Work\Perl\monks>perl use strict; use warnings; use autodie; my $data = <<EOD; Mary Had A Little Lamb She Was GOOD Not Changing this Or This Mary Had A Little Lamb She Was GOOD Line 9 Line 10 EOD open my $fh, '<', \$data; my $content = do { local $/; <$fh>; }; print "[[$content]] \n\n"; $content =~ s{ Mary \s+ Had \s+ A \s+ Little \s+ Lamb \s+ She \s+ Was \s+ GOOD } {CHANGED!!!}xmsg; print "<<$content>> \n"; __END__ [[Mary Had A Little Lamb She Was GOOD Not Changing this Or This Mary Had A Little Lamb She Was GOOD Line 9 Line 10 ]] <<CHANGED!!! Not Changing this Or This CHANGED!!! Line 9 Line 10 >>


    Give a man a fish:  <%-{-{-{-<