The key observation here is that you're making a copy of the file without those lines in it. There's no straightforward way to delete the lines directly from the file; instead you have to think about it slightly backwards:
the lines you want to keep, and just skip the ones you want to delete. When you're done, you can rename the new file onto the original, or you can use perl's