sed/awk: modify source file?

1

1

I'm trying to script some changes I need to make to a couple of config files. It's worth mentioning that I'm an expert with neither sed nor awk, though I'm far better with sed.

Right now, I'm doing:

sed '/setting.name/c setting.name: newvalue' </etc/foo.conf >tmpfile.tmp && mv tmpfile.tmp /etc/foo.conf

But surely there's a better way? Perhaps with awk?

Joe Fruchey

Posted 2013-05-31T12:02:51.073

Reputation: 55

2man sed tells me that you should use the -i, --in-place switch (if available in your version of sed. For awk there does not seem to be such an option...maybe you want to write a short script to do it. – Bobby – 2013-05-31T12:15:36.507

Argh. RTFM, Joe!! – Joe Fruchey – 2013-05-31T12:34:04.457

Answers

3

A better way:

 sed -i.bak 's/Old Info/New Info/' file.cfg

That edits the file in place, but saves the original as 'name.cfg.bak'.

If you know all the things to change, then you'd probably want a script to do it like:

 sed -i.bak -e 's/first setting/new first/' -e 's/next setting/new next/' -e 's/etc/new etc/' file.cfg

nerdwaller

Posted 2013-05-31T12:02:51.073

Reputation: 13 366