Start replacing sed commands with python

It turns out that OSX uses BSD sed and our merge script was written
for gnu sed. For portability, let's just convert all sed commands
to Python.