Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

RE: Applying a regex across multiple files

by nuance (Hermit)
on May 25, 2000 at 17:51 UTC ( [id://14748]=note: print w/replies, xml ) Need Help??


in reply to Applying a regex across multiple files

Updated from Q & A this is a futher answer to a post in Q & A Apply regex to entire file, not just individual lines ? which was turing into a conversation better suited to this forum.

I'm not sure exactly what problem you're still experiencing, but the following seems to do what you're asking for.

#!/usr/bin/perl -w my $raw = 'c:/temp/delete.me'; my $out = 'c:/temp/newfile'; my $lines; { open (RAW, "<$raw") or die "Couldn't open $RAW"; local $/ = undef; $lines = <RAW>; close RAW; } $lines =~ s/^unwantedstuff//msg; $lines =~ s/moreunwantedstuff//msg; open (OUT, ">$out") or die "Couldn't open $out for writing"; print OUT $lines; close OUT;
Note the added /g modifier, if you want to get all occurrances of a peice of text, you probably need that as well.

I put the part that reads the file into a block so that making the $/ variable local is restricted to that section of the program. That's probably overkill if your entire script is not much bigger than this. If you start writing longer scripts, the fact that you've changed the value of this predefined global could give you problems. If you put it in a separate block as I've done here, it isolates the rest of the program from the effects of that.

Nuance

Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://14748]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found