В perl такая операция может быть легко осуществлена множеством способов, не стесняйтесь выбирать ту, которая вам подходит
Заменяющий подход
use strict;
use warnings;
use feature 'say';
my $filename = 'config.xml';
my $insert =
'<vhostMap>
<vhost>google</vhost>
<domain>google.com, www.google.com</domain>
</vhostMap>';
open my $fh, '<', $filename
or die "Couldn't open $filename: $!";
my $data = do{ local $/; <$fh> };
close $fh;
$data =~ s/((.*?\n){8})/${1}$insert\n/s;
open $fh, '>', $filename
or die "Couldn't open $filename: $!";
say $fh $data;
close $fh;
Подход среза массива
use strict;
use warnings;
use feature 'say';
my $filename = 'config.xml';
my $insert = '<vhostMap>
<vhost>google</vhost>
<domain>google.com, www.google.com</domain>
</vhostMap>';
open my $fh, '<', $filename
or die "Couldn't open $filename: $!";
my @lines = <$fh>;
close $fh;
open $fh, '>', $filename
or die "Couldn't open $filename: $!";
say $fh @lines[0..7],$insert,"\n",@lines[8..$#lines];
close $fh;
Итерационный подход к массиву
use strict;
use warnings;
use feature 'say';
my $filename = 'config.xml';
my $insert = '<vhostMap>
<vhost>google</vhost>
<domain>google.com, www.google.com</domain>
</vhostMap>';
open my $fh, '<', $filename
or die "Couldn't open $filename: $!";
my @lines = <$fh>;
close $fh;
open $fh, '>', $filename
or die "Couldn't open $filename: $!";
my $line_count = 0;
for (@lines) {
chomp;
$line_count++;
say $fh $_;
say $fh $insert if $line_count == 8;
}
close $fh;
Вход
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
Выход
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
<vhostMap>
<vhost>google</vhost>
<domain>google.com, www.google.com</domain>
</vhostMap>
line 9
line 10
line 11
line 12