Gunnar Hjalmarsson schreef:
> Dr.Ruud:
>> Gunnar Hjalmarsson:
>>> PlagueMagazine:
>>>> I have data in a text file where the important thing I want
>>>> to extract is between two blank lines.
>>>
>>> Assuming the data has been slurped into $_ :
>>>
>>> print "$1\n" while /\n\n(.+)(?=\n\n)/g;
>>
>> Or /\n[[:blank:]]*\n(.+)\n[[:blank:]]*\n/
>> if "blank" includes blanks. :)
>
> Right. However, the zero-width assertion (?=pattern) is important for
> the case of two consecutive "important things".
>
> text
> text
>
> important line
>
> other important line
>
> text
> text
It is easy to code with the new Perl 5.10 regex functionalities,
but it can be done just as well with split:
#!/usr/bin/perl
use strict;
use warnings;
{ local ($\, $/) = ("\n",); # say + slurp mode
print for grep !/\n/, split /\n[[:blank:]]*\n|$/, <DATA>;
}
__DATA__
text
text
important line
other important line
text
text
important test
<EOF>
For big files, slurping ain't nice, but a flagging approach can be:
my $n = 0; # non-empty lines count
my $b = ""; # buffer
while (<DATA>) {
if (/^[[:blank:]]*$/) {
print $b if $n == 1; # prints lonely lines at start too
$n = 0; next
}
$b = $_ and ++$n;
}
print $b if $n == 1; # print lonely line at end
--
Affijn, Ruud
"Gewoon is een tijger."
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/