2012-07-09 37 views
0

我想搜索string另一个string取代它递归一个目录下。 例如: 我想搜索字符串如何搜索一个字符串,并用linux中的其他字符串替换它?

"var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");"

var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-7044592-1']);_gaq.push(['_setDomainName', 'news4u.com']);_gaq.push(['_trackPageview']);

更换它是如何实现的呢?

回答

0

使用find找到的所有文件,并xargs运行sed对它们进行修改:

find dir -type f | xargs sed -i.bak 's#from#to#' 

会找到下目录的所有文件,并从与取代,备份原扩展名为“.bak”的文件

NB from是一个基本的正则表达式,因此您需要转义您正在搜索的字符串中的任何BRE元字符。这将是在一个单一的文件来测试sed命令一个好主意,得到它的工作:

sed 's#from#to#' file > test.out 

,将做替代品,以文件但不是修改文件将输出写入到test.out所以你可以检查结果。

+0

我试过下面这段代码(我正在用'goo'来代替测试) 'find/home/project/new4u/apps/frontend/modules/alerts/templates/testing -type f -exec sed - 我的/ var gaJsHost =((\“https:\”== document.location.protocol)\?\“https:\/\/ssl。\”:\“http:\/\/www。\” )/ goo /“{} \;' 它工作不正常。我已经逃过了所有的分隔符,但仍然不起作用。请让我知道要做些什么改变。 – 2012-07-09 09:19:06

+0

你已经逃脱了太多,'?'不是BRE元字符。另外,如果你使用'''引号,那么你不需要转义'''字符,如果你使用#从#到#而不是's/from/to /',那么你不需要需要跳过'/'字符 – 2012-07-09 09:48:11

+0

非常感谢,我停止了逃避?“并且它工作。 – 2012-07-09 11:19:23

相关问题