You seem to be using BSD sed. You can say:
sed '/\[base\]/,/gpgkey=/{/gpgkey=/s/$/\'$'\n''exclude = postgres*/;}' filename
to append the line exclude = postgres*
after gpgkey=...
in the [base]
section.
EDIT: Explanation:
/\[base\]/,/gpgkey=/
matches an addresses range, i.e. lines starting from the one containing[base]
upto the one containinggpgkey=
{/gpgkey=/s/$/\'$'\n''exclude = postgres*/;}
is a group of commands that is executed only for the addresses matched above
Breaking down /gpgkey=/s/$/\'$'\n''exclude = postgres*/
:
- This performs the mentioned substitution
s/$/\'$'\n''exclude = postgres*/
on lines that matchgpgkey=
. - The above substitution matches
$
, i.e. the end of line. - The replacement is a newline followed by
exclude = postgres*/
$'\n'
is ANSI-C quoting syntax for producing a newline. Since you didn't seem to be using GNU sed, hence this was required. Else\n
would suffice, i.e. it could have been written as/gpgkey=/s/$/\nexclude = postgres*/