The hostlist.txt file has only 1 col. The prog reads hostlist.txt file, remove duplicate hostnames, sort the list, lookup ip address of each host in the list, and print the output on terminal.
hostlist.txt
host01
host03
host02
host01
output on terminal
host01,192.168.1.15
host02,192.168.1.12
host03,192.168.1.33
Program:
open(HOSTFILE, "hostlist.txt") or die "Couldn't open location file: $!\n";
while ($hosts = <HOSTFILE>) {
chomp($hosts);
push(@hostnames, $hosts);
}
close HOSTFILE;
@hostnameUnique = uniq(@hostnames);
@hostnameUniqueSorted = sort { lc($a) cmp lc($b) } @hostnameUnique;
foreach $hostname (@hostnameUniqueSorted){
$ipaddr = inet_ntoa((gethostbyname($hostname))[4]);
print "$hostname,$ipaddr\n";
}
I want to do the same thing as above, except the input file newhostlist.txt has 3 cols. Remove the duplicate hostname, sort first col($type), then sort 3rd col($location), then sort 2nd col($hostname), lookup ip address, and print output.
How do I process the multiple column array?
newhostlist.txt
dell,host01,dc2
dell,host03,dc1
hp,host02,dc1
dell,host01,dc2
Output:
dell,host03,192.168.1.33,dc1
hp,host02,192.168.1.12,dc1
dell,host01,192.168.1.15,dc2