in reply to Insert lines into specific stanza line
#!/usr/bin/perl -w use strict; my %add2stanza = ( '12.34.56.79' => "NEW DATA for 12.34.56.79", "12.34.56.80" => 'More new data for 12.34.56.80', ); while (<DATA>) { print; if (m/\<stanza\s+(.*)\>/) { my $number = $1; add_data($add2stanza{$number}) if $add2stanza{$number}; } } # not the best loop structure as the return is buried within # the loop but as a quick hack, shows some points about how # to do this... sub add_data { my $new_data = shift; while (<DATA>) { if (/\<\/stanza\>/) { print " $new_data\n"; print '</stanza>',"\n"; return; } else { print; } } } =prints <stanza 12.34.56.78> sl1 this is my line sl2 justanotherline </stanza> <line 1224> data </line> <stanza 12.34.56.79> sl1 this is my line sl2 justanotherline NEW DATA for 12.34.56.79 </stanza> <stanza 12.34.56.80> sl1 this is my line sl2 justanotherline More new data for 12.34.56.80 </stanza> <stanza 12.34.60.90> sl1 this is my line sl2 justanotherline </stanza> =cut __DATA__ <stanza 12.34.56.78> sl1 this is my line sl2 justanotherline </stanza> <line 1224> data </line> <stanza 12.34.56.79> sl1 this is my line sl2 justanotherline </stanza> <stanza 12.34.56.80> sl1 this is my line sl2 justanotherline </stanza> <stanza 12.34.60.90> sl1 this is my line sl2 justanotherline </stanza>
|
|---|