批量搜索和替换文件内容

九月 15th, 2009 Posted in Perl | 阅读次数: 499 次

制作:高进波
时间:2009-09-15

使用PERL工具执行全局搜索和替换,功能非常强大,可对文件进行批量查找替换.

1.选项-e,执行-e后面的代码
打印hello world
perl -e ‘print “hello,world!\n”‘

2.选项-pe 1 ,显示文件内容,后面是1,2,3的1
perl -pe 1 /etc/passwd

3.全局搜索和替换,内容输出到stdout,-pe ‘s/T/newT/’
perl -pe ‘s/root/gaojinbo/’ /etc/passwd

4.全局搜索和替换,直接更改文件内容
1) -pi -e ‘s/T/newT/’ 不备份
2) -pi.bak -e ‘s/T/newT/’ 备份文件为*.bak后更改
perl -pe.bak -e ‘s/root/gaojinbo/g’ /etc/passwd
后面有个g会全局替换,如果不加g只能替换一个

5.查看修改过的文件
cat /etc/passwd

注:此方法适合于安装PERL环境的任何系统,包括UNIX,LINUX,WINDOWS

完成!

相关日志:

Tags:

3 Responses to “批量搜索和替换文件内容”

  1. Candida Treatment Says:

    Recent Favourite Sites…

    [...]below you’ll see my links page http://www.yeastinfectiontreatmentcure.net/links/ with some good sites that I think you should definitely visit[...]…


  2. Resources Says:

    Interesting Posts…

    [...]some other related resources on the web that are worth viewing on this subject include[...]…


  3. angry birds season summer Says:

    You should check this out……

    [...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]………


留下您的脚印