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.


In reply to Re: Multiline search and replace in file(s) by haukex
in thread Multiline search and replace in file(s) by g_speran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.