I need to load a file containing IP address and hostname into an array so that I can convert IP addresses into hostnames when parsing a log file. Is it permissable to use the IP address as the array subscript?
My data file named 'hosttable' :
10.1.2.3 host1.com
20.2.3.4 host2.com
30.3.4.5 host3.com
I have been trying to use the following awk script to load the array and display it
awk ' FILENAME == "hosttable"
{
split($0,entry," "
hosttable[entry[$1]] = entry[$2]
next
}
{
IP = "20.2.3.4"
foundit = 0
for ( ip in hosttable )
if ( IP == ip ) {
print IP " IP address found"
print hosttable[ip]
foundit = 1
break
}
if ( foundit == 0 )
print IP " Not found"
}
' hosttable
Any help would be useful.
many thanks
My data file named 'hosttable' :
10.1.2.3 host1.com
20.2.3.4 host2.com
30.3.4.5 host3.com
I have been trying to use the following awk script to load the array and display it
awk ' FILENAME == "hosttable"
{
split($0,entry," "
hosttable[entry[$1]] = entry[$2]
next
}
{
IP = "20.2.3.4"
foundit = 0
for ( ip in hosttable )
if ( IP == ip ) {
print IP " IP address found"
print hosttable[ip]
foundit = 1
break
}
if ( foundit == 0 )
print IP " Not found"
}
' hosttable
Any help would be useful.
many thanks