Using sed to replace javascript code in multiple files
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
This very similar question produced a satisfactory answer inawk
: 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 stretchingsed
with multi-line replace
– py4on
Apr 24 '15 at 6:51
add a comment |
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
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
bash sed awk
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 inawk
: 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 stretchingsed
with multi-line replace
– py4on
Apr 24 '15 at 6:51
add a comment |
This very similar question produced a satisfactory answer inawk
: 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 stretchingsed
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
add a comment |
2 Answers
2
active
oldest
votes
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 '' ;
add a comment |
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" ;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 '' ;
add a comment |
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 '' ;
add a comment |
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 '' ;
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 '' ;
answered Apr 24 '15 at 6:44
angusangus
9,35123432
9,35123432
add a comment |
add a comment |
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" ;
add a comment |
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" ;
add a comment |
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" ;
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" ;
edited Apr 24 '15 at 7:11
answered Apr 24 '15 at 6:50
chaoschaos
36k977120
36k977120
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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