Rewrite sed commands to be less platform dependent.

MacOS doesn't ship with version of sed that includes (convenient)
GNU extensions. We need to rewrite sed commands into a little bit more
ugly form to make them less platform dependent.
Merge remote-tracking branch 'gkossakowski/master'