Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to extract the contents of tags across multiple files in a directory.

The information I am trying to extract lies between the <notes > and </notes> tags (which may or may not fall across lines. Once extracted I want to put the information into 1 file which has the information on each line (line returns stripped). This is what I have created so far, being new to Perl, I have added bits and pieces from scripts found on the site.

I am having difficulty removing the line returns from the final output file. Also my current output gives me text which is outside of the notes tag, but this is only from the second record.

Can anyone help?

#!/usr/bin/perl -w use strict; use File::Find; use HTML::TokeParser; ##Here define the directory to work across my $root_dir = 'c:/test1'; ##Search the directory, when a file is found run the sub. find(\&wanted, $root_dir); sub wanted { # if the extension fits... if ( /(LOG[^\n]*)|(REC[^\n]*)\.xml?/i ) { ##Grab the filename for error to screen if cannot open. my $input = $_; open (OUTPUT, ">>c:\\1-Actnte.txt"); open INPUT, "$input" or die "Cannot open $input"; select OUTPUT; $\ = "\n"; my $foundstart; while (<INPUT>) { chomp; next unless ($foundstart || /<notes[^>]*>/i); while (/<notes[^>]*>/i && ! $foundstart) { $_ =~ s/^.*?<notes [^>]*>$/\n<notes $1\/i; $foundstart++; next unless($_); } while ($_ =~ m|<notes[^\r\n]*</notes>|i) { $_ =~ s|^(.*?)</notes>.*$|$1|i; print if($_); last; } print; } close INPUT; } } close OUTPUT;

Replies are listed 'Best First'.
Re: Extracting information from multiple files in a directory
by BrowserUk (Patriarch) on May 09, 2003 at 04:05 UTC

    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
Re: Extracting information from multiple files in a directory
by chromatic (Archbishop) on May 09, 2003 at 05:34 UTC

    Is this valid XHTML? If so, you're in luck -- (untested) SAX to the rescue:

    #!/usr/bin/perl -w use strict; use XML::Parser; use File::Find; my $root_dir = 'c:/test1'; my $p = XML::Parser->new( Handlers => { Start => \&handle_elem_start, End => \&handle_elem_end, Char => \&handle_char_data, }); find(\&wanted, $root_dir); sub wanted { next unless /(LOG[^\n]*)|(REC[^\n]*)\.xml?/i; $p->parse( read_file( $File::Find::name ) ); } sub read_file { my $filename = shift; local (*IN, $/); open( IN, $filename ) or die "Cannot open $filename: $!\n"; return scalar <IN>; } my $in_notes; sub handle_elem_start { my ($p, $name) = @_; return unless $name eq 'notes'; $notes++; } sub handle_elem_end { my ($p, $name) = @_; return unless $name eq 'notes'; $notes--; } sub handle_char_data { my ($p, $text) = @_; return unless $notes; $notes =~ tr/\r\n/ /; print $notes; }

    It does kinda scare me that that felt natural.

Re: Extracting information from multiple files in a directory
by zby (Vicar) on May 09, 2003 at 08:22 UTC
    How about using the  .. operator? I mean something like that:
    while (<INPUT>) { if(/<notes[^>]*>/i .. /<\/notes>/i){ strip the tags; print; } }