http://www.perlmonks.org?node_id=799603


in reply to How to write a new line in a while loop?

If I understand you correctly, you are wanting to do the equivalent of:

grep -v IPADDR "$dir/$sys/$ifdir/$if0" > "$dir/$sys/$ifdir/${if0}.new" echo "IPADDR=1.1.1.1" >> "$dir/$sys/$ifdir/${if0}.new"

at the command line. If this is the case, do you really need perl for this or is this part of a larger script/problem?

--gsiems

Update: Looking closer, I see that I missed the BOOTPROTO line. Is this closer to what you are looking for (not tested):

print "Please enter the ip address for this machine. This will be eth +0:\n"; my $ip = <>; chomp $ip; my $if0 = "ifcfg-eth0"; my $if1 = "ifcfg-eth1"; my $ifdir = 'network-scripts'; my $ifsub1 = "IPADDR="; my $ifsub2 = "BOOTPROTO="; my $ifsub3 = "none"; my $IP; my $NEW; open ($IP, "<", "$dir/$sys/$ifdir/$if0"); open ($NEW, ">", "$dir/$sys/$ifdir/$if0.new"); my @ary = ( grep { $_ !~ m/IPADDR/ } (<$IP>)); s/($ifsub2).+$/$1$ifsub3/ for @ary; print $NEW @ary; print $NEW "IPADDR=$ip\n"; close $IP; close $NEW;