O Ye Monks, I plea to be enlighten'd
I need to parse a set of files written by report programs. Each file has some sections delimited by before- and after- lines. Every section's contents will receive a different treatment. An example is given below. What I want to have is a *very* simple method to extend the parser, so as to account for future file formats. While future formats may have different section selector strategies, I want to have this pretty general case set down first.
---------------------------------------------------------------------- +----- @--- Callsites: 2 ---------------------------------------------------- +----- ---------------------------------------------------------------------- +----- ID Lev File/Address Line Parent_Funct MPI_Call 1 0 0x8048ad5 [unknown] Reduce 2 0 0x8048a3b [unknown] Bcast ---------------------------------------------------------------------- +----- @--- Aggregate Time (top twenty, descending, milliseconds) ----------- +----- ---------------------------------------------------------------------- +----- Call Site Time App% MPI% COV Bcast 2 9.5 24.71 65.75 0.59 Reduce 1 4.95 12.87 34.25 1.35 ---------------------------------------------------------------------- +-----
What I have devised up to now is something like the attached. That should parse the presented sample. Adding parsers should be a matter of writing proper RE for start and end lines, and then deciding what to do with each line belonging to the section. This parsing template would go in some config file or whatever.
As you can see, delimiter lines must not enter into process(). While my foreach loop may not be the most clever thing, it sorta works for me (suggestions gladly accepted).
Now, this works OK when only one parser is add()ed. However, with two parsers, I get the following output for the sample enclosed:#!/usr/bin/perl -w package Parser; use strict; sub new { bless {}; } sub add { my $self = shift; my $hash = shift; push @{$self->{parsers}}, $hash; } sub parse { my $self = shift; my $file = shift; open FILE,'<',$file || die "Can't open $file"; while(<FILE>) { foreach my $p (@{$self->{parsers}}) { if(/$p->{start}/ ... /$p->{end}/) { $p->{process}($_) unless (/$p->{start}/ || /$p->{end}/); } } } close FILE; } 1; sub parse1 { my $text = shift; print "Into parse1\n"; print $text; } sub parse2 { my $text = shift; print "Into parse2\n"; print $text; } my $p = Parser::new(); $p->add({ start => "^ ID Lev File/Address", end => "---", process => \&parse1}); $p->add({ start => "^Call *Site *Time *App", end => "---", process => \&parse2}); $p->parse($file);
Into parse2 ID Lev File/Address Line Parent_Funct MPI_Call Into parse1 1 0 0x8048ad5 [unknown] Reduce Into parse2 1 0 0x8048ad5 [unknown] Reduce Into parse1 2 0 0x8048a3b [unknown] Bcast Into parse2 2 0 0x8048a3b [unknown] Bcast Into parse1 Bcast 2 20 35.85 64.45 0.76 Into parse2 Bcast 2 20 35.85 64.45 0.76 Into parse1 Reduce 1 11 19.78 35.55 1.31 Into parse2 Reduce 1 11 19.78 35.55 1.31
So:
Thank you very much in advance
In reply to Parsing text sections by betacentauri
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |