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

Hello hoopy froods,

I've got a number of html files, each with some data that I want to copy, contained within tags like so:

<!--TABLE --> Table with lots of data here.... <!--/TABLE -->
I want to merge the tables into one long table, so that I can see all the data on one page, but I'm not sure where to start. Can you supply me with any sort of clue, because I am totally clue-less...

Thank you in advance for being wonderful people *crawl crawl*

KatGirl

Replies are listed 'Best First'.
Re: Copying text between markers
by tommyw (Hermit) on Mar 13, 2003 at 12:10 UTC

    perl -n -e 'print if /<!--TABLE -->/../<!--\/TABLE -->/' files
    with the usual provisos about having nested tables.

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

Re: Copying text between markers
by broquaint (Abbot) on Mar 13, 2003 at 12:15 UTC
    perl -ne 'print if /<!--TABLE -->/ .. m|<!--/TABLE -->/|' *.tpl > data
    That should give you a grab most if not all the data from *.tpl and redirect it into a file called data.
    HTH

    _________
    broquaint

Re: Copying text between markers
by valdez (Monsignor) on Mar 13, 2003 at 13:02 UTC

    This piece of code is not perfect, but demonstrates a different approach using HTML::TokeParser::Simple:

    #!/usr/bin/perl use strict; use warnings; use HTML::TokeParser::Simple; use File::Find; find(\&wanted, 'pages'); exit; sub wanted { my ($open, $buffer); return unless -f $_; my $p = HTML::TokeParser::Simple->new( $_ ) or die "can't parse $_: +$!"; while (my $token = $p->get_token) { if ($open) { if ($token->is_comment && $token->as_is =~ /\/table/i) { $open = 0; } else { $buffer .= $token->as_is; } } else { if ($token->is_comment && $token->as_is =~ /table/i) { $open = 1; } } } print $buffer, "\n"; }

    HTH, Valerio

Re: Copying text between markers
by zby (Vicar) on Mar 13, 2003 at 12:12 UTC
    First write a sub to scan all the files - using File::Find for example. Then use something like
    if($line =~ /<!--TABLE -->/ .. $line =~ /<!--\/TABLE -->/){ add_the_line_to_result($line)); }
    to get all the lines between the markers.
Re: Copying text between markers
by katgirl (Hermit) on Mar 14, 2003 at 11:56 UTC
    Thank you all! I got some great ideas from that - I eventually used a while loop though:
    #!/usr/bin/perl -w print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); opendir(DIR, "."); @files = grep( /\.tmp/, readdir(DIR) ); @files = sort(@files); closedir(DIR); foreach $file(@files){ $found = 0; open(FOO, "$file") or die "Cannot open. $!\n"; while (<FOO>){ print "Copying table from $file...<br>"; if ( $_ =~ m/<!--TABLE -->/ig){ $found = 1; } if ($found == 1){ open(FILE,">>filename2.txt") or die "Cannot open. $!\n"; print FILE $_; close(FILE); } if ( $_ =~ m/<!--\/TABLE -->/ig){ $found = 0; } } close FOO; }