Server Fault is a question and answer site for system and network administrators. 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

My configuration file, server.conf, has the following format

[general]
setting1=sdfdsf
setting2=asdfadsf

[ssl]
setting1=sadfsdf
setting2=sdfadsfkljasdf

How do I add an additional setting to the "general" stanza using a bash script?

share|improve this question
up vote 2 down vote accepted

You could do it with sed

$ sed 's/^\[general\]/\[general\]\nnew=setting/' file 

which adds new=setting after the [general]. using sed -i.bak ... file will do it in place and create a file.bak for safety.

share|improve this answer
    
usually you want to add new settings to the end of a section, can you add an example for that? – rubo77 Jul 21 at 8:15

This seems to be mostly answered over on stackoverflow:

http://stackoverflow.com/questions/6284518/how-to-insert-a-line-using-sed-before-a-pattern-and-after-a-line-number

Basically its a job for SED or AWK and a little regex.

share|improve this answer

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.