Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
up vote 3 down vote accepted

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
share|improve this answer
    
Thanks. That's perfect! – Joe Fruchey May 31 '13 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .