在Perl中,replacement命令清空Windows下的文件
我有以下命令:在Perl中,replacement命令清空Windows下的文件,windows,perl,shell,Windows,Perl,Shell,我有以下命令: perl -pi.bak -e "s/pattern//" $file_name 当我在perl脚本中执行它时,它会清空文件(我看到.bak文件,它不是空的)。当我在shell中执行它时,它就工作了。有什么问题吗 问题是在Windows下,它在Linux下工作正常 编辑1: 我也试过这个,它也空了: sub replace_pattern_in_file { my ($file, $pattern, $replacement) = @_; open my $i
perl -pi.bak -e "s/pattern//" $file_name
当我在perl脚本中执行它时,它会清空文件(我看到.bak文件,它不是空的)。当我在shell中执行它时,它就工作了。有什么问题吗
问题是在Windows下,它在Linux下工作正常
编辑1:
我也试过这个,它也空了:
sub replace_pattern_in_file
{
my ($file, $pattern, $replacement) = @_;
open my $in, "<", $file;
unlink $file;
open my $out, ">", $file;
while(my $line = <$in>)
{
$line =~ s/$pattern/$replacement/g;
print $out $line;
}
}
子替换\u文件中的\u模式
{
我的($file,$pattern,$replacement)=@;
在“,”中打开我的$文件;
while(我的$line=)
{
$line=~s/$pattern/$replacement/g;
打印$out$行;
}
}
你错了;这在Windows和unix系统上都是一样的。在任何一种类型的系统上,只有当模式完全匹配每一行(包括换行符)时,才能获得所描述的行为
你的模式是什么?重新编辑1,这在Windows上不起作用,因为它没有匿名文件。
>echo foo>file
>echo bar>>file
>echo moo>>file
>perl -pi.bak -e "s/o//" file
>type file
fo
bar
mo