in reply to Appending data to a file in a specific location

Loop through the lines of your input file, search for your magic string with a regular expression, then inject the extra lines. Something like this should get you started:
use strict; use warnings; while (<DATA>) { if (/ip helper-address/) { print; print " ip helper-address x.x.x.x\n"; print " ip helper-address x.x.x.x\n"; } else { print; } } __DATA__ ! interface GigabitEthernet1/0/1 description *** VLAN 1 ** no switchport ip address x.x.x.x x.x.x.x ip helper-address x.x.x.x no ip redirects no ip proxy-arp service-policy input U_SET_DSCP ip ospf cost 10 srr-queue bandwidth share 1 3 97 1 priority-queue out standby ip x.x.x.x standby timers 1 3 standby priority 95 standby preempt !

Replies are listed 'Best First'.
Re^2: Appending data to a file in a specific location
by nemasys (Initiate) on May 05, 2009 at 09:44 UTC

    Hi,

    Thanks for the reply, I had used this code before, but I should have explained that you might get multiple ip helper-address statements one below the other and if you have multiple, then you will be applying the print statement more than once which is not the what I need. Is there a way to just apply the two new ip helper-address statments below more than one that has been already defined

    Example:

    !
    interface GigabitEthernet1/0/1
     description *** VLAN 1 **
     no switchport
     ip address x.x.x.x x.x.x.x
     ip helper-address x.x.x.x
     ip helper-address x.x.x.x
     no ip redirects
     no ip proxy-arp
     service-policy input U_SET_DSCP
     ip ospf cost 10
     srr-queue bandwidth share 1 3 97 1
     priority-queue out 
     standby ip x.x.x.x
     standby timers 1 3
     standby priority 95
     standby preempt
    !
    

    Thanks in advance

    James