The file isn't huge. In that case, I wouldn't bother with the optimization.
The file isn't gigantic. In that case, I'd slurp in the file into a string, and attempt the modification. Exit if nothing changed.
If the file is gigantic (so it won't fit into memory), I'd use 'grep' to see if there's an occurrence. If not, no need to even start the program. Else, run the program.