查找并替换字符串的脚本


首先判断输入的参数是否是两个

查找 所有的xml配置文件 把文件中从头到尾含有参数1($1)全部替换为参数2($2)
使用perl不生成转换过程中的中间文件,执行起来很快 -i参数直接原地编辑文件。

然后把参数1 替换为参数2

#!/bin/bash
if [ $# -lt 2 ]; then
 echo pls Input 2 parameter
else
 echo replace \"$1\" with \"$2\"
 find . -name "*.xml" -print | xargs perl -pi -e "s/$1/$2/g"
fi
exit 0

使用find和sed

#首先查找个数(sed -n)

find . -name '*.htm' | xargs sed -n '/old/p'

#然后替换(sed -i)

find . -name '*.htm' | xargs sed -i 's/old/new/g'


使用grep和sed

grep -l old *.htm | xargs sed -n "/old/p"
sed -n '/old/p' `grep -l old *.htm`


使用find和perl

find . -name '*.htm' | xargs  perl -pi -e 's|old|new|g'


参考:

http://hi.baidu.com/msingle/blog/item/3b125b3fc0e1efe255e72375.html

http://blog.csdn.net/zhangboyj/article/details/6215429

// 使用 shell 在多个文件中查找  
find . -type f | xargs grep flash  
说明:  
找出前目录及其子目录下的文件中含有 flash 的文件  
//  
重点记录下 xargs 命令:  
xargs 在标准输入上取得参数列表、一行一个,再将他们以适当的大小组起来(由主机的 ARG_MAX 值决定)传给另一个命令,此命令在作为 xargs 的参数.  
ARG_MAX 值查看方法:  
getconf ARG_MAX  
//  
//  
在多个文件中查找使用命令替换也可以完成  
grep flash $( find . -type f)  
说明:  
$() 是命令替换,括号内是 shell 命令;shell 执行括号内的命令并将整个 $(...) 替换为 命令的输出  
命令替换的形式有两种,一种是 $(),另一种是使用两个反引号 `find . -type f`

// 多个文件中替换 - sed  
为了保证不该替换的不被替换,先在文件中查找一下哪些会被替换  
find -type f ! -path '*.svn*' | xargs sed -n '/Ookong inc/p'  
说明:  
1, find 首先过滤掉 .svn 目录及其子目录  
2, xargs 传递参数给 sed 命令  
3, sed 执行查找, 只打印符合要求的行  
//  
// 替换  
find -type f ! -path '*.svn*' | xargs sed -i 's/Ookong inc/Ookong Ltd. Co./g'  
说明:  
1, sed 的 i 选项直接在文件中替换,而不是把替换结果打印出来  
2, g 对每行中所有匹配执行替换  
//  
//  
另外:一个 perl 多文件查找的程序: ack  
-- 显示所在的行号及所在的文件 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
options: -s single-line mode -m multi-line mode -i ignoreCase -G global -R from right to left -E extended-regexp pattern ERE -e ="PAT-regexp" -C ="strToOperate" -F fixed-strings fgrep -c ="num" print only a count of matching first lines per file -n force the prefixing filename on output -N without prefixing filename on output -o show only the part of the line that matched -f ="file" read regex patterns from file -q quiet suppress all normal output,stderr etc -O output file offsets, not text -x print line number with output lines -r recursively scan sub-directories -I select only non-matching lines -S ="replaceto" Match(regexp)-->replaceto -p ="bak-suffix-name",Modify file(S) directly,will create backup file(s) if bak- suffix-name is not null -d ="dir to search" default=currentDirectory,dir-str can't end with \",avoid esc ape " -X mask filename to be operated with regex -T only output filename and match num for per file search file:if set the -e arg and no (-C arg || stdin-pipe || inputfile),then ou tput filepath that match -ergx;if -o arg is set,output filenames only 从命令行中-C参数指定要处理的字符串: C:\>E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -Chello2012 -e2\d+2 Line:1 Offset:5 hello2012 //output -o参数指定只输出匹配部分: C:\>E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -Chello2012 -e2\d+2 -o 2012 从标准输入 管道里获取要处理的数据 C:\>help |E:\MyProjects1117\deelxGrepSed\Debug\deelxgrep.exe -e^V.+?\s -o VERT VE VER VER VERIFY VOL 从文件里读取数据处理,支持递归处理目录 E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -ewindows Boo-t.ini -i Boo-t.ini result : Line:3 Offset:45 (0)partition(1)\WINDOWS [operating sys Line:5 Offset:37 (0)partition(1)\WINDOWS="Microsoft Wind Line:5 Offset:56 DOWS="Microsoft Windows XP Professional -d指定目录,-r表示递归处理 -e指定要查找的表达式,如果没有带处理数据(即没有-C\管道输入\指定文件)但指定了目录,则会 处理目录下的目录名和文件名,并输出符合表达式的路径名称 E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -eboot.i*i -i -d"c:" -r c:\boot.ini E:\MyProjects1117\deelxGrepSed\Release>deelxgrep.exe -eboot.i*i -i -d"c:\\"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值