我正在写一个小的perl程序,我正在检查#start和#end的模式。议程是用开始和结束模式之间的线创建一个单独的文件。这我可以用下面的脚本来做。替换写入文件描述符的最后一行
#!/usr/bin/perl
open(INFILE,"<","testcases") || die "Can't open file: $!";
my $binary;
my $tccounter=1;
while(<INFILE>)
{
if(/^#start/i)
{
open(OUTFILE,">",$tccounter."_case.sh") || die "Can't open file: $!";
print "start of the script\n";
next;
}
elsif(/^#end/i)
{
################################
# Want to replace the previously
# written line here with some
# addtional customized lines
################################
close(OUTFILE);
$tccounter++;
print "End of the script\n";
print "last line for this testcase is \n $binary\n";
next;
}
else
{
$binary=$_ unless(/^\s*$/);
print OUTFILE $_;
}
}
但我还需要的是为确定被写入到文件的最后一行,然后替换一些自定义的数据,额外的线。 例如,在我的情况下,所有文件的最后一行是执行的。 我想在所有输出文件中替换“execute”行。 在电流输出文件的最后一行如下:
execute
有望走出文件的最后一行应该是
preline
execute
postline
输入文件(测试用例):
#start
line1
line 2
execute
#end
#start
line3
line 4
execute
#end
#start
line5
line 6
execute
#end
#start
line7
line 8
execute
#end
*您必须*在您编写的每个Perl程序的顶部使用strict和'use warnings'all''。使用'my'来声明变量没有'use strict'就没有什么意义了。 – Borodin