2

I need to replace an old google analytics code with the new universal tracking code in lots of static html files. The old code looks like this:

_uacct = "UA-XXXXXX-X"; urchinTracker(); 

The new code is the following:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-XXXXXX-X', 'xxx.com');ga('require', 'displayfeatures');ga('send', 'pageview'); 

I'm trying to combine bash find command and sed (CentOS 6):

find docs/ -type f -iname "*.html" -exec sed -i ':a;N;$!ba;s/_uacct = "UA-XXXXXX-X";\nurchinTracker();/(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,\'script\',\'\/\/www.google-analytics.com\/analytics.js\',\'ga\');ga(\'create\', \'UA-XXXXXX-X\', \'xxx.com\');ga(\'require\', \'displayfeatures\');ga(\'send\', \'pageview\');/g' {} \; 

However, this produces the following error:

-bash: syntax error near unexpected token `(' 

After some research I found the brackets should be escaped as well:

find arts/ -type f -iname "bass.html" -exec sed -i ':a;N;$!ba;s/_uacct = "UA-XXXXXX-X";\nurchinTracker();/\(function\(i,s,o,g,r,a,m\){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function\(\){\(i[r].q=i[r].q||[]\).push\(arguments\)},i[r].l=1*new Date\(\);a=s.createElement\(o\),m=s.getElementsByTagName\(o\)[0];a.async=1;a.src=g;m.parentNode.insertBefore\(a,m\)}\)\(window,document,\'script\',\'\/\/www.google-analytics.com\/analytics.js\',\'ga\'\);ga\(\'create\', \'UA-XXXXXX-X\', \'xxx.com\'\);ga\(\'require\', \'displayfeatures\'\);ga\(\'send\', \'pageview\'\);/g' {} \; > > 

As you may see bash thinks something is not escaped, breaks the line and does not execute the command.

What I'm doing wrong?

If this is possible to do with another tool (let's say awk)?

2
  • This very similar question produced a satisfactory answer in awk: unix.com/shell-programming-and-scripting/…
    – toxefa
    CommentedApr 24, 2015 at 6:46
  • I'd suggest a proper script in perl or python though.. You're going to be stretching sed with multi-line replace
    – toxefa
    CommentedApr 24, 2015 at 6:51

2 Answers 2

1

You can't escape single quotes inside single quotes. The easiest way for you is to put your script in a file and then run

find arts/ -type f -iname "bass.html" -exec sed -i -f your-script.sed '{}' \; 
    0

    That worked for me

    find docs/ -type f -iname "*.html" -exec \ perl -i.bak -0pe 's/_uacct = "UA-XXXXXX-X";\nurchinTracker\(\);/test/igs' {} \; 

    Notice that -i.bak makes a copy of the original file with extension .bak, in case anything goes wrong. The replacement is now the string test. Replace that with your long replacement.

    With your huge replacement it would then look like:

    find docs/ -type f -iname "*.html" -exec perl -i.bak -0pe "s/_uacct = \"UA-XXXXXX-X\";\nurchinTracker\(\);/\(function\(i,s,o,g,r,a,m\)\{i\['GoogleAnalyticsObject'\]=r;i\[r\]=i\[r\]\|\|function\(\)\{\(i\[r\].q=i\[r\].q\|\|\[\]\).push\(arguments\)\},i\[r\].l=1\*new Date\(\);a=s.createElement\(o\),m=s.getElementsByTagName\(o\)\[0\];a.async=1;a.src=g;m.parentNode.insertBefore\(a,m\)\}\)\(window,document,'script','\/\/www.google-analytics.com\/analytics.js','ga'\);ga\('create', 'UA-XXXXXX-X', 'xxx.com'\);ga\('require', 'displayfeatures'\);ga\('send', 'pageview'\);/igs" {} \; 

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.