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

Hi all,
I've matched a string in a text file, but I what to skip and read the next dozen or so lines.
Don't quite know how to do this simple task.

At the moment i have tried using <STDIN> and chomp to read the next line after the matched string, but with no success.

Is there a command that tells perl to move to the next line and read it ?.

cheers

Edit kudra, 2002-05-17 Changed title

  • Comment on how do I get perl to skip lines reading a text file?

Replies are listed 'Best First'.
Re: next line
by broquaint (Abbot) on May 16, 2002 at 13:08 UTC
    Are you looking for readline()?
    my $line = <FH>; $line = readline(*FH) if $line =~ /$some_regex/;
    Although this is exactly the same as doing <FH> in the given context. Maybe some code would help?
    HTH

    _________
    broquaint

      how about if I just wanted to move to the next line regardless of what it contained, would I use the same sort of command ?
        Yes you would. When you assign a read from a filehandle in a scalar context the file pointer will be incremented to the next line according to the $/ variable e.g
        while(<FH>) { my $nextline = <FH>; }
        What happens here is that the first line of <FH> is assigned to $_ and the filehandle will point to the second line. When $nextline is assigned the read from <FH> it gets the second line, and the filehandle will then point to the 3rd line, and so on and so forth.
        HTH

        _________
        broquaint

Re: next line
by ph0enix (Friar) on May 16, 2002 at 13:24 UTC
    $line=<FILE> if $line =~ /regexp/;

    Is it what are you searching for?

Re: next line
by mrbbking (Hermit) on May 16, 2002 at 16:28 UTC
    To skip to the next line, after having found what you're looking for...
    open( FH, $yourfile ) or die "Can't open $yourfile: $!\n"; while( <FH> ){ next if /pattern_indicating_line_to_skip/; # otherwise, do stuff... }