Using sed to replace javascript code in multiple files

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)?










share|improve this question
























  • 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















1















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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)?










share|improve this question
























  • 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













1












1








1








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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)?










share|improve this question
















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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)function()(i[r].q=i[r].q,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)?







bash sed awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 8:34









Rui F Ribeiro

41.9k1483142




41.9k1483142










asked Apr 24 '15 at 6:24









SraySray

1214




1214












  • 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

















  • 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
















This very similar question produced a satisfactory answer in awk: unix.com/shell-programming-and-scripting/…

– py4on
Apr 24 '15 at 6:46





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





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










2 Answers
2






active

oldest

votes


















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 '' ;





share|improve this answer






























    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])(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








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f198298%2fusing-sed-to-replace-javascript-code-in-multiple-files%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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 '' ;





      share|improve this answer



























        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 '' ;





        share|improve this answer

























          0












          0








          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 '' ;





          share|improve this answer













          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












          share|improve this answer



          share|improve this answer










          answered Apr 24 '15 at 6:44









          angusangus

          9,35123432




          9,35123432























              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])(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





























                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])(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



























                  0












                  0








                  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])(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















                  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])(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














                  share|improve this answer



                  share|improve this answer








                  edited Apr 24 '15 at 7:11

























                  answered Apr 24 '15 at 6:50









                  chaoschaos

                  36k977120




                  36k977120



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f198298%2fusing-sed-to-replace-javascript-code-in-multiple-files%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay