I want to add a line
allow = alaw
before a string
nat = no
in a file sip.conf (or any text based file). If allow = alaw already exists before nat = no it should not be added. And the line should not be added in the two sections immediately after [general] and [providertrunk0] of the file.
Example file content:
[general]
disallow = all
allow = ulaw
nat = no
[providertrunk0]
disallow = all
allow = ulaw
;allow = alaw
nat = no
secret =
nat = no
progressinband = yes
allow = ulaw
allow = alaw
nat = no
progressinband = yes
disallow = all
allow = ulaw
nat = no
progressinband = yes
After changes it would be
[general]
disallow = all
allow = ulaw
nat = no
[providertrunk0]
disallow = all
allow = ulaw
;allow = alaw
nat = no
secret =
allow = alaw
nat = no
progressinband = yes
allow = ulaw
allow = alaw
nat = no
progressinband = yes
disallow = all
allow = ulaw
allow = alaw
nat = no
progressinband = yes
My attempt
I tried want to exclude the whole process for [general] and [providertrunk0] section, but apply in whole file, using '/general/,/providertrunk0/{next}' like this:
awk -v add="allow = alaw" '/general/,/providertrunk0/{next} /^nat = no$/&&lastLine!=add{print add}{lastLine=$0}1' sip.conf '
but its not giving correct output.
secret =looks like the 2nd section. Am I wrong?