如何使用sed替换以下文本? [英] How to replace the following text using sed?
问题描述
我想替换 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/脚本%3E"));
与
(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' ==document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();
在所有情况下...
所以我使用了下面的代码.
So i used this below code.
find /cygdrive/c/xampp/htdocs/news -type f -exec sed -i s#document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));#(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();#g {} \;
但它并没有取代.逃跑有问题吗?
But it is not replacing. Is there any problem in escaping ?
谢谢
推荐答案
您需要引用 sed 的参数,并转义任何嵌套的引号.否则这个:
You need to quote the argument to sed, and escape any nested quotes. Otherwise this:
s#document.write(unescape("%3Cscript src='" + gaJsHost + ...
将被 shell 分解成单独的单词,而不是作为单个参数传递给 sed.
will be broken into separate words by the shell, instead of being passed as a single argument to sed.
您需要用引号将整个 sed 脚本(s#from#to#
部分)括起来,我会选择单引号,然后替换每个 '
中的带有 \'
的脚本.
You need to surround the entire sed script (the s#from#to#
part) in quotes, I would choose single quotes, then replace every '
in the script with \'
.
(另外,你为什么使用 find -exec
而不是我建议的?)
(Also, why are you using find -exec
instead of what I suggested?)
这篇关于如何使用sed替换以下文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!