in reply to editing a file
You will at least need to read the line that needs to be checked. This will probably require reading the file up to that point. However, it may be cheaper from a memory point of view to read the file to that point (not saving it), determine if the change is needed, then seek back to the beginning and process the file if the change is needed (no reason to close it in this case). Then you can:<bl>
Write the edited version to a temporary file
Rename or unlink the original file
Rename the temporary file with the original name</bl>
I was going to suggest the -i switch until I thought about it a bit more and realized that you probably don't want to always write if the editing is not necessary (and I don't know about the seekability of the <> input, either).