Replace Google Analytics code in a few hundred static pages?
===

I was hoping to create a bash script and use sed to replace old google analytics code with new but haven't had any success yet, currently getting "unterminated substitute pattern" errors when using the below code. Also I'm not sure I can use sed to replace blocks of text on multiple lines like this, maybe I have to use awk?

    OLDANALYTICS='<script type=\"text/javascript\">
    var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");
    document.write(unescape(\"%3Cscript src='\''\" + gaJsHost + \"google-analytics.com/ga.js'\'' type='\''text/javascript'\''%3E%3C/script%3E\"));
    </script>
    <script type=\"text/javascript\">
    try {
    var pageTracker = _gat._getTracker(\"UA-111111-5\");
    pageTracker._trackPageview();
    } catch(err) {}</script>'
    
    NEWANALYTICS='<script type=\"text/javascript\">
                var _gaq = _gaq || [];
                _gaq.push(['\''_setAccount'\'', '\''UA-111111-5'\'']);
                _gaq.push(['\''_trackPageview'\'']);
    
                (function() {
                    var ga = document.createElement('\''script'\''); ga.type = '\''text/javascript'\''; ga.async = true;
                    ga.src = ('\''https:'\'' == document.location.protocol ? '\''https://ssl'\'' : '\''http://www'\'') + '\''.google-analytics.com/ga.js'\'';
                    var s = document.getElementsByTagName('\''script'\'')[0]; s.parentNode.insertBefore(ga, s);
                })();
    
            </script>'
    
    sed -i '' "s#$OLDANALYTICS#$NEWANALYTICS#g" $1

I'll have to incorporate this with a 'find' command once it is working, to replace text in all files recursively.