How to replace a range of text with sed or awk?

Howdy!

I'm trying to automate editing of a configuration file (custom.conf for GDM). I need to find every line between a line that starts with "[daemon]" and the next line that starts with "[". When I find "[daemon]", I want to preserve that line, but then delete all the lines in that configuration section and then insert several new lines of text between the first match ("[daemon]") and the second match ("]").

I suspect that I can do this with sed or at least awk, but I'm not quite sure where to start Can anyone suggest which tool might be better suited to this task and provide some code that accomplishes what I'm talking about?

Thanks,
Tad

Show an example please and use CODE tags, ty.

I have the following file:

# Some comments
# S'more comments

[daemon]
TimedLoginEnable=true
TimedLoginDelay=10
TimedLogin=user1
# and maybe some other, unpredictable lines that I don't need or want

[security]

[xdmcp]

[gui]
...

I wish for it to become something like

# Some comments
# S'more comments

[daemon]
TimedLoginEnable=true
TimedLoginDelay=5
TimedLogin=user2

[security]

[xdmcp]

[gui]
...

So far, I have managed to come up with this awk script:

BEGIN {
    searching = 1; 
    done = 0
}
searching == 1 {
    print;
    if ($1 == "[daemon]") {
        searching = 0;
        print "TimedLoginEnable=true";
        print "TimedLoginDelay=5";
        print "TimedLogin=user2";
        print "";
    }
}
searching == 0 {
    if (done) {
        print;
    } else {
        getline;
        if (match($0, /^\[/)) {
            print;
            done = 1;
        }
    }
}

But I feel like the awk script should be more concise, and I also suspect that this can be done in sed, but I don't even know where to start on that one.

Thanks,
Tad

Clearing the lines between [daemon] and the next starting with [...

awk '/^\[daemon\]$/ {print; i=1; next} /^\[/ && i == 1 {print; i=0; next} i == 1 {next} i == 0 {print}' infile> outfile

Having your new values saved in an extra file called "replacement"; adding it's contents and writing it to "newfile":

awk '/^\[daemon]/ { print; while ((getline _ < "replacement") > 0) {print _} } {print} ' outfile > newfile

Thanks for the last part to prevent writing the last line 2 times goes to radoulov taken from this thread Adding a file at a particular place in another file