Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found