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?

  • 2
    man 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 May 31 '13 at 12:15
  • Argh. RTFM, Joe!! – Joe Fruchey May 31 '13 at 12:34
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.