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

I need to extract lines from an output file that have characters such as * - eg: ** ------------------------ Actual --------------------** and ** Summary: 329 passed, 23 failed, 16 exceptions, 0 warning ** and n following lines and use them as body of email. Any assistance greatly appreciated

Replies are listed 'Best First'.
Re: regex/extracting whole lines
by George_Sherston (Vicar) on Oct 19, 2001 at 16:34 UTC
    May I suggest you post your attempt so far - that would make it easier to give an intelligent answer to your question as (A) one could then see the problem more clearly (B) one wouldn't be telling you stuff you already know.

    § George Sherston
Re: regex/extracting whole lines
by CubicSpline (Friar) on Oct 19, 2001 at 16:49 UTC
    Is something like this what you're after:
    use strict; open IN, "filename" || die "Couldn't open file!"; my $line,$i; my $n = 5; #how many lines after the **line** to get my $emailtext = ""; while( chomp($line = <IN>) ) { #if line has ** in it, then get next n lines if( $line =~ /\*\*/) { for my $i (1..$n) { $emailtext .= <IN>; } #send your email here } }

    HTH
    ~CS

Re: regex/extracting whole lines
by zuqif (Hermit) on Oct 19, 2001 at 16:51 UTC
    mysql import!?
    if ($_ =~ /^\*\*/) { for (1..$n) { $mail_msg .= <FH>; } }
    the 'qif;
Re: regex/extracting whole lines
by Hofmator (Curate) on Oct 19, 2001 at 17:26 UTC
Re: regex/extracting whole lines
by strfry() (Monk) on Oct 19, 2001 at 17:48 UTC
    here's my version of it:
    #!/usr/bin/perl use warnings; use strict; my ($line,$i,$n,$emailtext); while( $line=<DATA> ) { if( $line =~ m/^\*\*$/) { $emailtext .= <DATA> if(*DATA); next if ( $line =~ m/^\*\*$/ ); } } print "$emailtext\n"; # you could start your print MAIL # stuff here. __DATA__ ** She turned me into a newt! ** One day, a shell shall emerge from the masses, written in Perl, and it will be Good. ** A newt?! ** The people will rejoice, and it shall be dubbed 'SuperShell'. ** I got better. **
    i wasn't entirely certain as to what you were trying to accomplish, though, so this might not be very useful. ):

    strfry()
      Thanks to all of you and apologies for not being clearer. I have a script that makes and builds the distribution for various apps and outputs results as .txt files. What I really need is to extract various results/summaries etc from the output files, strip of characters such as '*' and '-' and, as I'm using a cmd line mailer, set the results/summaries as variables. Again, sorry for not being clearer
        hmm can you maybe paste an example of the output file (pre-regex), and if possible, show us how you want the output to appear (post-regex)?
        eg: if your file looks like so -
        ** - blah blah blah - **
        and you want it to look like this:
        blah blah blah
        then it'd be a very simple fix.

        strfry()