> 我现在用的WangKevin的办法,要有多个关键字要替换,我感觉perl -i -pe 's/root/rainbird/g' passwd
> ,可能要多次打开文件,效率低些。
>
每个替换对应一个 s///; 不就可以了。 Perl 命令行的用途还是很多的,建议看
看这篇文章
http://wiki.perlchina.org/Perl命令行应用介绍
Qiang
>
> 2009/11/5 Rainbird <chinakapalink@gmail.com>:
>> perl -i -pe 's/root/rainbird/g' passwd
>> 希望你能从这句话中得到灵感:)
>>
>> 2009/11/5 Ruixian Zhu <zhurx4g@gmail.com>
>>> 好 多谢
>>>
>>> 2009/11/5 WangKevin <kevin-w-sh@hotmail.com>:
>>>> 直接写回去就覆盖了
>>>>
>>>> undef $/;
>>>>
>>>> open FILE, $filepath;
>>>> $C = <FILE>;
>>>> close FILE;
>>>> $/="\n";
>>>>
>>>> $C =~ s/str1/str2/gi
>>>>
>>>> open( FILE, ">$filepath");
>>>>
>>>> print FILE $C;
>>>> close(FILE);
>>>>
>>>> 你也可以试试 >+
>>>> 打开一次看行不行
>>>>
>>>>
>>>> MALA WANG
>>>>
>>>>
>>>>
>>>>
>>>> ________________________________
>>>> Date: Thu, 5 Nov 2009 13:40:00 +0800
>>>> Subject: [PerlChina] Re: 请教下,perl如何在替换文件里的字符串并写回文件
>>>> From: smallfish.xy@gmail.com
>>>> To: perlchina@googlegroups.com
>>>>
>>>> 替换完直接把内容写入当前文件就可以了
>>>> --
>>>> http://pythonchina.org
>>>> http://hi.baidu.com/smallfish_xy
>>>>
>>>>
>>>> 2009/11/5 Nestle <zhurx4g@gmail.com>
>>>>
>>>> 请教下,perl如何在替换文件里的字符串并写回文件。我现在的做法是,打开一个文件,替换内容,写到新文件,最后删除旧文件,用原文件名重新命名新文
>>>> 件,有没有直接写回的办法?我不想创建新的文件。
>>>>
>>>>
>>>>
>>>
>>
>>
>> --
>> 态度决定高度,技术成就梦想
>>
>
> >
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina Mongers 讨论组"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---
没有评论:
发表评论