1

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?

Yuri
  • 220
  • 1
  • 6
travis1097
  • 113
  • 1
  • 3

3 Answers3

3

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.

Khaled
  • 36,533
  • 8
  • 72
  • 99
user9517
  • 115,471
  • 20
  • 215
  • 297
  • usually you want to add new settings to the end of a section, can you add an example for that? – rubo77 Jul 21 '16 at 08:15
0

Can you try as given below

cat server.conf
[general]
setting1=sdfdsf
setting2=asdfadsf

[ssl]
setting1=sadfsdf
setting2=sdfadsfkljasdf

sed  '/general/,/^$/s/^$/setting3=new_entry\n/g' server.conf

cat server.conf
[general]
setting1=sdfdsf
setting2=asdfadsf
setting3=new_entry

[ssl]
setting1=sadfsdf
setting2=sdfadsfkljasdf
P Ekambaram
  • 101
  • 2
0

This seems to be mostly answered over on stackoverflow:

https://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.

WerkkreW
  • 5,969
  • 3
  • 24
  • 32