2012-07-09 40 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

相關問題