Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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. Could you please advise what I'm doing wrong?

If this is possible to do with another tool (let's say awk), please advise.

Thanks in advance.

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

2 Answers 2

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 '{}' \;
share|improve this answer

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" {} \;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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