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

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.
  • Comment on Re^4: Delete 10 lines before matched line

Replies are listed 'Best First'.
Re^5: Delete 10 lines before matched line
by ikegami (Patriarch) on Mar 11, 2010 at 19:31 UTC

    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.