The simplest way of extracting stuff that straddles line boundaries would be to slurp the whole file in to memory and remove the newlines ( $data =~ tr[\n][]d; works well) before applying your regex.

However, if your files are too large to fit in memory, then you need to buffer sufficient lines to encompass your data (with the newlines stripped) to allow you to extract the data, and then discard the bits you have matched before appending more and repeating the cycle.

The following code shows one way to acheive this. As demonstrated, it reads & appends just 2 bytes at a time, until a match is found and then repeats. Hardly efficient, but it demostrates the technique rather nicely of you uncomment the print statement at the top of the outer while loop. You should be able to choose whatever size buffer suits your needs. Around 64k seems a fairly good place to start.

#! perl -slw use strict; my $re_notes = qr[<notes>.*?</notes>]; my $buffer = ''; while (sysread( DATA, $buffer, 2, length $buffer)) { $buffer =~ tr[\n][]d; # print $buffer; while($buffer =~ m[($re_notes)]og ) { print $1; } $buffer = substr($buffer, 1+rindex($buffer, '</notes>')); } __DATA__ Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap Loads'a junk a garbage and <notes>This is the text I am looking for split across several lines</notes>irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap Loads'a junk a garbage<notes>This is the text I am looking for on one +line embedded</notes> and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap <notes>This is the text I am looking for on one line on its own</notes +> Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap Loads'a junk a garbage and irrelevent crap <notes>This is the text I am looking for split across two lines</notes> Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and +irrelevent crap

Output

D:\Perl\test>256751 <notes>This is the text I am looking for split across several lines</n +otes> <notes>This is the text I am looking for on one line embedded</notes> <notes>This is the text I am looking for on one line on its own</notes +> <notes>This is the text I am looking for split across two lines</notes +>

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

In reply to Re: Extracting information from multiple files in a directory by BrowserUk
in thread Extracting information from multiple files in a directory by Anonymous Monk

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.