举个简单的例子:
open IN,"f:/a.txt"||die $! #读入文件
open OUT,">f:/b.txt"||die $!#输出文件
$marker=0
$x=5#要插入的行
while(<IN>){
$marker++
if($marker==5){
print OUT "5\tlala\n"
}
print OUT $_
}
当然你可把他写成一个函数,传参进去,那样更加灵活。希望有帮助~~。
#!/usr/bin/perl
$file = shift
$matched = 0
$newFile = "balabalabala_tmp"
die "usage: ./test.pl filename\n" if !defined $file
open FD, "<$file" or die "can't open $file:$!"
open TMP, ">$newFile"
select TMP
while(<FD>) {
$line = $_
if(!$matched &&/nameserver/) {
print "nameserver 8\n"
$matched = 1
}
print $line
}
close FD
close TMP
unlink $file
rename $newFile, $file
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)