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

I'm trying to conceptualize how I can take a file and treat blocks of that data as one entity. I just need a nudge in the right direction.
I don't have any code, but here's a sample of the text file I need to parse:


Wednesday April 14, 2004. >> HOSTNAME Universal scustom "\"hostname\"" HOSTNAME ABC RUNNING DEF RUNNING GHI RUNNING ************************** End HOSTNAME>>HOSTNAME2 Universal scusto +m "\"hostname\"" HOSTNAME2 ABC RUNNING DEF RUNNING GHI RUNNING ************************** End HOSTNAME2


I want to treat the text between the ">>" and "**End HOSTNAME" as 1 block of data that I can then parse through looking for certain elements. And yes, the file really is this ugly.

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: capturing blocks of data
by graff (Chancellor) on Jun 10, 2004 at 23:25 UTC
    If you're using "HOSTNAME" in your post as a sort of "placeholder" for a wide range of different strings (like "abc.def.com", "college.univ.edu", etc), then some of the replies above will be off the mark.

    On the other hand, if the ">>" that precedes the first occurrence of each HOSTNAME string is reliable, and if that pattern occurs only as the initial two characters of a new record, then just set perl's record separator variable ($/) to ">>". That way, the standard while loop with the diamond operator will read one full record at a time.

Re: capturing blocks of data
by Roy Johnson (Monsignor) on Jun 10, 2004 at 18:08 UTC
    $/='** End HOSTNAME';
    Then, if necessary, strip off everything before >> in each record.

    The PerlMonk tr/// Advocate
Re: capturing blocks of data
by McMahon (Chaplain) on Jun 10, 2004 at 18:30 UTC
    I hope I read the question right. This is untested, but I think it does the right thing:

    use File::Slurp; my $big_text = read_file ('bigFile.txt'); my @blocks = split/HOSTNAME/, $big_text; foreach my $block(@blocks) { print "$block\n"; }
    Hope that helps...
Re: capturing blocks of data
by hardburn (Abbot) on Jun 10, 2004 at 17:47 UTC

    You'll probably need to break out a full-fledged parser, like Parse::RecDescent. Start reading :)

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.