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;

In reply to 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.