如果这不是一个重复,我会感到惊讶,但我似乎找不到解决这个问题的任何地方。我试图用另一个字符串替换文件中给定字符串的所有实例。我遇到的问题是,脚本打印替换的版本,但保留原始版本。我对perl非常陌生,所以我确信这是一个很小的问题,而且我遗漏了一些东西
代码:
my $count;
my $fname = file_entered_by_user;
open (my $fhandle, '+<', $fname) or die "Could not open '$fname' for read: $!";
for (<$fhandle>) {
$count += s/($item_old)/$item_new/g;
print $fhandle $_;
}
print "Replaced $count occurence(s) of '$item_old' with '$item_new'\n";
close $fhandle;原始文件:
This is test my test file where
I test the string perl script with test
strings. The word test appears alot in this file
because it is a test file.结果文件:
This is test my test file where
I test the string perl script with test
strings. The word test appears alot in this file
because it is a test file
This is sample my sample file where
I sample the string perl script with sample
strings. The word sample appears alot in this file
because it is a sample file.预期结果文件:
This is sample my sample file where
I sample the string perl script with sample
strings. The word sample appears alot in this file
because it is a sample file.附加信息:
$item_old和$item_new由用户提供。在给出的示例中,我将test替换为sample。发布于 2014-02-09 23:32:08
问题在于,您正在使用+<模式,认为它会做您认为它所做的事情。您要做的是首先读取文件中的所有行,将文件句柄位置放在文件末尾,然后打印后面的行。
这条线
for (<$fhandle>) {读取文件句柄的所有行,并将它们放入一个列表中,然后循环在列表上迭代。它一直读取到eof,并且只在之后添加更改。
如果要使解决方案正常工作,则必须在打印之前将文件句柄倒转。也就是说。
seek($fhandle, 0, 0);虽然这种解决办法不是很好,但在我看来。特别是当有内置功能来处理这类事情时:
perl -pi.bak -we 's/$item_old/$item_new/g' yourfile.txt带有-i标志的-p标志使您的代码被应用到文本文件中,并相应地修改它,并使用扩展名.bak保存副本。当然,您必须提供您想要的替代,因为您没有提供它。
编辑:,我刚看到你不想要一条线。好的,要完成这个一行程序所做的工作,您只需要打开正确的文件句柄,并将更改的文件复制到旧的文件上。所以,基本上:
use strict;
use warnings;
use File::Copy;
open my $old, "<", $oldfile or die $!;
open my $new, ">", $newfile or die $!;
while (<$old>) {
s/$item_old/$item_new/g
print $new $_;
}
copy $newfile, $oldfile or die $!;大多数情况下,使用允许在同一个文件句柄上读写的模式比使用这些模式要复杂得多,考虑到处理文件副本是多么容易。
https://stackoverflow.com/questions/21666386
复制相似问题