最佳答案
我知道这种问题以前已经被问过很多次了。我之所以再次来到这里是因为我觉得我错过了一些简单而基本的东西。
有没有可能使这种搜索替换例程更好。例如,不打开同一个文件两次。也欢迎有关速度的建议。
请注意,这适用于多行匹配并替换多行字符串。
#!/bin/perl -w -0777
local $/ = undef;
open INFILE, $full_file_path or die "Could not open file. $!";
$string = <INFILE>;
close INFILE;
$string =~ s/START.*STOP/$replace_string/sm;
open OUTFILE, ">", $full_file_path or die "Could not open file. $!";
print OUTFILE ($string);
close OUTFILE;