Centos中查詢並替換某個目錄下所有檔案中的某個字串
阿新 • • 發佈:2018-12-07
-
查詢目錄下包含某個字串的檔案
例:查詢 dir 目錄下所有包含 str 的檔案。
grep -lr 'str' dir
-
vim替換單個檔案中所有字串方法
例:替換當前檔案中所有 old 為 new
:%s/old/new/g
-
替換目錄下所有檔案中到某個字串——sed結合grep
例:要將目錄 dir 下面所有檔案中的 old 都修改成new,這樣做:
sed -i "s/old/new/g" `grep 'old' -rl dir`
-
批量替換多個檔案中的字串的簡單方法。
用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"