linux 搜索並替換文件內容

一、指定目錄 terry 下的全部文件內容中,把 terry 替換爲 breakeast ,此處爲 正則替換,須要進行轉義處理html

find ./terry -type f | xargs perl -i -pe s%terry%breakeast%gshell

 

二、指定替換文件類型bash

find ./terry "*.html" -type f | xargs perl -i -pe s%terry%breakeast%gide

 

三、若要替換時,同時生成備份,則須要寫shell腳本實現:htm

#!/bin/bash
str="www.breakeast.com"
newstr="blog.breakeast.com"
for i in `find ./terry/`
do
grep "$str" $i
if [ $? == 0 ]
then
echo $iblog

cp $i $i.newfile
sed "s/$str/$newstr/g" $i 
fi
doneget

源於青互聯博客http://www.qing.es博客

相關文章
相關標籤/搜索