Использование awk:
$ echo network.com www 192.168.10.10 |
awk '
NR==FNR {
a=$2 # store hostname
b=$3 # and ip
next # .
}
$1==a { # if hostname matches
sub(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,b) # replace ip looking string
}1' - a_file # output
Выход:
www IN A 192.168.10.10
webmail IN A 192.168.10.2
mail IN A 192.168.10.3
Редактировать
Версия, которая добавляет несоответствующую запись в конец:
$ echo network.com www2 192.168.10.10 |
awk '
NR==FNR {
a=$2 # store hostname
b=$3 # and ip
next # not needed for this input but good practise
}
FNR==1 { t=$0 } # store a template record for later use
$1==a { # if hostname matches
sub(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,b) # replace ip looking string
f=1 # flag up when there was replace
}
1; # output
END { # in the end
if(!f) { # if there was no replace
sub(/^[^ \t]+/,a,t) # replace the template
sub(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,b,t)
print t # and output it
}
}' - a_file
Выход:
www IN A 192.168.10.1
webmail IN A 192.168.10.2
mail IN A 192.168.10.3
www2 IN A 192.168.10.10