I often want to automate this sort of task in a shell script:
if the line:
SOMEKEY=SOMEVALUE
exists in a file, then change it to
SOMEKEY=SOMEOTHERVALUE
otherwise, append the line SOMEKEY=SOMEOTHERVALUE
in the file.
How could I go about this? I think I could do it using a combination of grep
and sed
, but I'm sure it's a common enough task that someone has already worked out an elegant solution.
By the way, when replacing I would normally do something like this
sed -i 's/old/new/g' fname
But it means I have to be very careful when composing my regular expressions, so as not to make a mistake. Is there an easy way to "preview" what changes which would occur from my call to sed
without actually stomping on the file?