in reply to Re: Delete 10 lines before matched line
in thread Delete 10 lines before matched line

# Discard the buffered lines. @buffer = (); # Print the matched line. print $line;
should be
# Discard the buffered lines. @buffer = $line;

Replies are listed 'Best First'.
Re^3: Delete 10 lines before matched line
by jmcnamara (Monsignor) on Mar 11, 2010 at 16:47 UTC

    Yes it should.

    I thought of that and thought that the OP probably didn't want the matched line deleted or they would have said so. In which case I thought that they would only want 10 lines or less deleted back to the last match. Which leaves me doing a lot of ass of you and me-ing.

    However, your modification is definitely more correct in the general case.

    --
    John.

      thanks John it worked. in the while loop you are reinitializing the buffer so that In the else loop it goes and prints the contents ..Iam still learning perl, so can you please let me know if I have understood something wrong.

        You can't erase from a file, you can simply recreate it with modifications. In this case, the modification you want is to avoid copying certain lines to the new file. The catch is that you don't know if a line should be copied or not when you read it.

        The solution is to delay copying a line until you know it should be copied. @buffer holds the last ten lines encountered. Older lines are copied to the new file once it becomes known it's safe to copy them. Recent lines are removed from the buffer when it becomes known they shouldn't be copied.