#!/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 () { print; if (m/\/) { 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 () { if (/\<\/stanza\>/) { print " $new_data\n"; print '',"\n"; return; } else { print; } } } =prints sl1 this is my line sl2 justanotherline data sl1 this is my line sl2 justanotherline NEW DATA for 12.34.56.79 sl1 this is my line sl2 justanotherline More new data for 12.34.56.80 sl1 this is my line sl2 justanotherline =cut __DATA__ sl1 this is my line sl2 justanotherline data sl1 this is my line sl2 justanotherline sl1 this is my line sl2 justanotherline sl1 this is my line sl2 justanotherline