in reply to replace part of a line with part of the following line after if is TRUE
Just one additional remark on top of the previous posts.
Since you are opening this file for output, IN is a very poor choice of identifier.