Reading the file, modifying and writing back is really not so bad of a solution. Especially if there is a lot to change, as seems to be in your case.
IMHO, this provides a clean solution (you can have a backup of the original file easily, for exmaple), that is relatively easy to implement.
So, unless you have a very special case in which you *must not* read-process-write the file, use this solution.