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

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.

  • Comment on Re^5: Delete 10 lines before matched line