[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Matching the Next Line(s)



At 3:11 PM +0200 5/17/00, Sean Carte wrote:
>How would I go about matching a string in a line, then returning 
>both that line and the next 2 lines?
>
>I'm happy with the basic match, it's getting it to return the next 
>line(s) that's got me stumped:
>
>--
>#! /usr/bin/perl -w
>
>use strict;
>
>my $file = 'text.txt';
>my $string = 'hello world';
>my ($matches, $line);
>
>open(IN, $file)
>     or die("Could not open $file: $!\n");
>
>while($line = <IN>) {
>     if ($line =~ /$string/gi) {
>	$matches .= $line . '\n'; # and the next 2 lines if there are any

Since you're not chomping the lines, this should work:

         $matches = $line . <IN> . <IN>;

>     }
>}
>
>close(IN);
>
>if ($matches) {
>     print "$matches\n";
>} else {
>     print "No matches to $match were found in $file";
>}
>--
>
>Any ideas?
>--
>My brain hurts!
>SeanC
>                      Mediatek Training Institute
>            129 Moore Road, Berea, Durban, South Africa
>    phone: +27 (0)31 305 4200              SeanC@mediatek.co.za
>      fax: +27 (0)31 305 4236
>                   <http://members.xoom.com/s_carte>
>
># ===== Want to unsubscribe from this list?
># ===== Send mail with body "unsubscribe" to macperl-request@macperl.org

-- 
--
Paul Schinder
schinder@pobox.com

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org