Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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