perl中,怎么修改文件中的某一行
1个回答
展开全部
process_line("log.log", 10, sub {return "line 100\n";});
sub process_line{
my ($f, $n, $rep_call) = @_;
my $H;
open($H, $f) || die "can not open $f\n";
my $l = 0;
my $found = 0;
my $str;
while (<$H>) {
$l++;
if ($l == $n) {
$found = 1;
$str .= &$rep_call($_);
}
else {
$str .= $_;
}
}
close $H;
return if ($found == 0);
open $H, "+>", $f;
print $H $str;
close $H;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询