in reply to Lines above and Below a matched string

One way is to read your file in chunks of three lines. If line 2 matches the format you need, then you go ahead and process lines 1 & 3, otherwise go to the next chunk of three.

-Tats
  • Comment on Re: Lines above and Below a matched string

Replies are listed 'Best First'.
Re: Re: Lines above and Below a matched string
by derby (Abbot) on Jun 26, 2003 at 18:46 UTC
    Kinda yes and kinda no. If line 2 doesn't match, you have then make line three the first line of your buffer and read in two lines.

    -derby

Re: Re: Lines above and Below a matched string
by Itatsumaki (Friar) on Jun 26, 2003 at 19:30 UTC

    derby pointed out the problem with my approach. I think BrowserUK's idea of sliding buffers is probably best, but you can salvage my approach by opening three file-handles and having them in different frames. In code:

    open(IN1, "<$infile"); open(IN2, "<$infile"); open(IN3, "<$infile"); my $temp; $temp = <IN2>; # remove one dummy line from 2nd file-handle $temp = <IN3>; $temp = <IN3>; # remove two dummy lien from 3rd file-handle # Now process all three file-handles separately # in chunks of three lines

    And there was a recent node on how to read a file in chunks of n lines, that may help.

    -Tats