ArifS has asked for the wisdom of the Perl Monks concerning the following question:
It printsuse warnings; use strict; my $file =<<END; object-group network HOSTNAME-1A_1ST network-object host 10.1.1.1 object-group network HOSTNAME-1B_2ND network-object host 10.1.1.2 object-group network HOSTNAME-2A_1ST network-object host 10.3.1.1 object-group network HOSTNAME-2B_2ND network-object host 10.3.1.2 object-group service WEB_TCP tcp port-object eq 80 END my $hostname; open my $fh, '<', \$file or die "unable to open read file $!"; while (my $line = <$fh>) { if (my ($name) = $line =~ /^object-group network (\w+)/) { $hostname = $name; # "last seen hostname" } my ($ip) = $line =~ /(\d+\.\d+\.\d+\.\d+)/; if (defined ($ip)) { print "ip $ip goes with host name $hostname\n"; } }
Please let me know.ip 10.1.1.1 goes with host name HOSTNAME ip 10.1.1.2 goes with host name HOSTNAME ip 10.3.1.1 goes with host name HOSTNAME ip 10.3.1.2 goes with host name HOSTNAME
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Match IP from 2nd line and Print Hostname from 1st line
by Corion (Patriarch) on Aug 01, 2016 at 12:52 UTC | |
by ArifS (Beadle) on Aug 01, 2016 at 13:33 UTC | |
by Marshall (Canon) on Aug 01, 2016 at 17:31 UTC | |
by ArifS (Beadle) on Aug 02, 2016 at 14:39 UTC |