首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl文件regex不替换文本

Perl文件regex不替换文本
EN

Stack Overflow用户
提问于 2014-02-09 23:10:12
回答 1查看 274关注 0票数 2

如果这不是一个重复,我会感到惊讶,但我似乎找不到解决这个问题的任何地方。我试图用另一个字符串替换文件中给定字符串的所有实例。我遇到的问题是,脚本打印替换的版本,但保留原始版本。我对perl非常陌生,所以我确信这是一个很小的问题,而且我遗漏了一些东西

代码:

代码语言:javascript
复制
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;

原始文件:

代码语言:javascript
复制
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.

结果文件:

代码语言:javascript
复制
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.

预期结果文件:

代码语言:javascript
复制
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
  • 我对这个问题不感兴趣。它将与一个更大的程序集成,这样可以从终端运行的一线解决方案就不会太有帮助了。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 23:32:08

问题在于,您正在使用+<模式,认为它会做您认为它所做的事情。您要做的是首先读取文件中的所有行,将文件句柄位置放在文件末尾,然后打印后面的行。

这条线

代码语言:javascript
复制
for (<$fhandle>) {

读取文件句柄的所有行,并将它们放入一个列表中,然后循环在列表上迭代。它一直读取到eof,并且只在之后添加更改。

如果要使解决方案正常工作,则必须在打印之前将文件句柄倒转。也就是说。

代码语言:javascript
复制
seek($fhandle, 0, 0);

虽然这种解决办法不是很好,但在我看来。特别是当有内置功能来处理这类事情时:

代码语言:javascript
复制
perl -pi.bak -we 's/$item_old/$item_new/g' yourfile.txt

带有-i标志的-p标志使您的代码被应用到文本文件中,并相应地修改它,并使用扩展名.bak保存副本。当然,您必须提供您想要的替代,因为您没有提供它。

编辑:,我刚看到你不想要一条线。好的,要完成这个一行程序所做的工作,您只需要打开正确的文件句柄,并将更改的文件复制到旧的文件上。所以,基本上:

代码语言:javascript
复制
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 $!;

大多数情况下,使用允许在同一个文件句柄上读写的模式比使用这些模式要复杂得多,考虑到处理文件副本是多么容易。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21666386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档