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博客

相关文章
相关标签/搜索