Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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

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.