2009年11月4日星期三

[PerlChina] Re: 请教下,perl如何在替换文件里的字符串并写回文件

是这么写吗?
sub replaceInFile{
print "Process:".$_[0];
open TARGET_FILE,$_[0];

while(defined($line=<TARGET_FILE>)){
$count=0;
while($count<scalar(@keyword)){
$_=$line;
s/@keyword[$count]/@replacekeyword[$count]/g;
$line=$_;
$count++;
}
print TARGET_FILE $line;
}

close TARGET_FILE;
}

2009/11/5 Rainbird <chinakapalink@gmail.com>:
> -i?
>
> 2009/11/5 smallfish <smallfish.xy@gmail.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 访问该论坛
-~----------~----~----~----~------~----~------~--~---

没有评论: