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)?

  • This very similar question produced a satisfactory answer in awk: unix.com/shell-programming-and-scripting/… – py4on Apr 24 '15 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 – py4on Apr 24 '15 at 6:51
0

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" {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.