[Gllug] Extracting a range of Lines from a file

Simon Wilcox simonw at simonwilcox.co.uk
Thu Mar 21 13:52:54 UTC 2002


On Thu, 21 Mar 2002, Jackson, Harry wrote:

> Hi all
>
> Im afraid its another Perl question.
>
> If I want to extract a range of lines on a text match I was thinking along
> the lines of the following.
>
> while ($current = <EXTRACT>) {
>
> 		if ($current =~ /^(.*?12\/12\/2000.*?$/ ..
> /^(.*?12\/01\/2001.*?)$/ ) {
> 		    Do stuff on the range here;
> 			}
> 		}

I would do something like this (not tested) :


my $startdate = "12/12/2000";
my $enddate = "13/01/2000"; # Note date you want to STOP matching
my $found=0;

while ($current = <EXTRACT>) {

    # Skip unless we've found the start of this is the first match
    next unless $found || $current =~ m#$startdate#;

    # Jump out if we match the end date
    last if $current =~ m#$enddate#;

    # Increment the found counter
    # becomes true on the first match and keeps a handy tally
    $found++;

    # Do stuff here
}

Of course, this assumes that you're log file is in date order.

HTH,

Simon.

-- 
"Counterpoint the surrealism of the underlying metaphor"



-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list