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


In reply to RE: Applying a regex across multiple files by nuance
in thread Applying a regex across multiple files by nuance

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.