in reply to redo entire loop

It doesn't sound like you want to redo the same thing as the first pass... It sounds like you want to do a second pass of the file while doing everything differently.

I suggest extracting the common logic fragment (inspecting the line) into a subroutine.

However, if your files aren't too big, you could simply modify the records in memory, then write it out only if you made changes.