/<begin>/{
insert_file("before_file.html")
print $0
insert_file("after_file.html")
next
}
{
print $0
}
where you will have to write the insert_file function which might look something like
function insert_file(file) {
while (getline line <file)
print line
close(file)
}
Note that this exact version doesn't seem to be working as expected on my Mac when before_file and after_file are the same...I'm only getting the only the first copy. It probably has something to do with failing to close the file. I shall investigate. Yes, it is necessary to close the file, and that should be done in general for good practice.
Also, I think this might be even easier in sed...
For inserting the file after the key line
sed '/<begin>/r after_file.html' input_file
Inseting the file before is a little more complicated,
sed -n -e '/^function/r before_file.html' -e 'x' -e 'p' input_file
so you could use a script like
/^function/r before_file.html
x
p
with
sed -n -f script input_file