For loop that will replace the ifcfg data.

Change IP to multiple servers based on a database file. Source ip file is not incremental.

# Source host
#cat hosts

server1
server2
server3
server4
server5
server6

# SOURCE file
#cat ip

Host                  IP             Netmask               GW
server1        192.0.0.1        255.255.255.0        192.0.0.0
server2        192.0.0.2        255.255.255.0        192.0.0.0
server3        192.0.0.3        255.255.255.0        192.0.0.0
server4        192.0.0.4        255.255.255.0        192.0.0.0
server5        192.0.0.5        255.255.255.0        192.0.0.0
server6        192.0.0.6        255.255.255.0        192.0.0.0
server7        192.0.0.7        255.255.255.0        192.0.0.0
server8        192.0.0.8        255.255.255.0        192.0.0.0

# file to be replace from server1

DEVICE=eth0
BOOTPROTO=dhcp
#HWADDR=
IPV6INIT=no
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
#UUID=
USERCTL=no
#IPADDR=10.1.1.16
#NETMASK=255.255.254.0

#File from server 2

DEVICE=eth0
BOOTPROTO=dhcp
#HWADDR=
IPV6INIT=no
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
#UUID=
USERCTL=no
#IPADDR=10.8.6.6
#NETMASK=255.255.250.0
#GATEWAY=10.0.0.0

Expected Output:
Server1:

DEVICE=eth0
BOOTPROTO=none
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.0.0.1
NETMASK=55.255.255.0
GATEWAY=192.0.0.0

Server2:

DEVICE=eth0
BOOTPROTO=none
MTU=1500
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.0.0.2
NETMASK=55.255.255.0
GATEWAY=192.0.0.0
for ip in `cat ip` ;do while read ip net gw
do

For starters, try:

while read host ip net gw
do
  ....
done < ip

or if you want to skip the first line that contains a header:

{
  read
  while read host ip net gw
  do
    ....
  done
} < ip
1 Like

How about this:

hosts=( $(cat hosts) )
for host in ${hosts[@]}
do
    grep "^$host" ip | {
        read server ip net gw
        sed '/NETMASK/d;/IPADDR/d;/GATEWAY/d;/^#/d' $host
        echo "IPADDR=$ip"
        echo "NETMASK=$net"
        echo "GATEWAY=$gw"
    } > ${host}_new
done

This reads your server1 file and produces server1_new with commented lines and existing NETMASK, IPADDR and GATEWAY lines removed and appends new lines with values from ip file.
Its not very efficient as it re-reads ip for every host.