centos中尋找並替換某個目錄下所有檔案中的某個字串

來源:互聯網
上載者:User


因為某種原因項目中的某個方法丟失,而很多檔案引用了該方法,需要將其全部替換為一個新的方法,引用的次數很多,手動改太麻煩.所以需要一次性替換.

例如要將目錄 dir 下面所有檔案中的 old 都修改成 new:

sed -i "s/old/new/g" `grep 'old' -rl dir`

1、尋找目錄下包含某個字串的檔案

例:尋找 dir 目錄下所有包含 str 的檔案。


grep -lr 'str' dir 

2、vim替換單個檔案中所有字串方法

例:替換當前檔案中所有 old 為 new


:%s/old/new/g 

3、替換目錄下所有檔案中到某個字串——sed結合grep

例:要將目錄 dir 下面所有檔案中的 old 都修改成new,這樣做:


sed -i "s/old/new/g" `grep 'old' -rl dir` 

4、批量替換多個檔案中的字串的簡單方法。

用sed命令可以批量替換多個檔案中的字串。
               
sed -i "s/原字串/新字串/g" `grep 原字串 -rl 所在目錄`
例如:我要把mahuinan替換為huinanma,

 

執行命令:sed -i "s/mahuinan/huinanma/g" 'grep mahuinan -rl /www'

這是目前linux最簡單的批量替換字串命令了!
具體格式如下:

sed -i "s/oldString/newString/g" `grep oldString -rl /path`

 執行個體代碼:sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl /usr/aa`
sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl ./`
這裡要注意,相關要進行轉義:

執行個體:


sed -i "s/<script src=\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.4\/

query.min.js\" type=\"text\/javascript\"><\/script>/ /g"

`grep '<script src="http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.4\/

jquery.min.js" type="text\/javascript"><\/script>' -rl test/`
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.