Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AIX awk array subscript question

Status
Not open for further replies.

jonnl0715

Programmer
Oct 23, 2003
2
FR
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
 
try something like this:

echo '10.1.2.3' | nawk -f johnl.awk
echo '10.1.2.6' | nawk -f johnl.awk
echo '20.2.3.4' | nawk -f johnl.awk

#-------------------- johnl.awk
BEGIN {
hostFile="hosttable"

FLD_hostIP=1
FLD_hostNAME=2

FLD_dataIP=1

while (getline < hostFile > 0) {
# skip comments in the config file
if ( $0 ~ /^[#].*/ ) continue;
arrConf[$FLD_hostIP]=$FLD_hostNAME
}
close(hostFile);
}


{
printf(&quot;[%s] - %s\n&quot;, $FLD_dataIP, (($FLD_dataIP in arrConf) ? arrConf[$FLD_dataIP] : &quot;Not found&quot;));
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
hello Vlad,

that worked, many thanks for your help.

Cheers/Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top