Q:

sed: replace a block of text

I have a bunch of files, starting with a block of code and I'm trying to replace with another. 
Replace:
<? php
$r = session_start();
(more lines)

With:
<? php
header("Location: index.php");
(more lines of code)

So im trying to match the block with sed 's/<?php\n$r = session_start();/<?php\nheader... but it doesn't work.  
I would appreciate help in what is happening here and how to achieve this.  I'm thinking in doing this with python instead. 
Thanks! 

A:

This might work for you (GNU sed):
sed -i '1i\
This is a\
new block of\
code
1,/$r = session_start();/d' file 

Or if you prefer to place the new code in a file:
sed -i '1r replacement_code_file
1,/$r = session_start();/d' file

All on one line:
sed -i -e '1r replacement_code_file' -e '1,/$r = session_start();/d' file

