Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Match IP from 2nd line and Print Hostname from 1st line

by ArifS (Beadle)
on Aug 01, 2016 at 12:49 UTC ( [id://1168917]=perlquestion: print w/replies, xml ) Need Help??

ArifS has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am trying to modify the following code from Marshall, but it doesn't seem to work.
Trying to print the Hostname for the IP from the 2nd line.
use 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"; } }
It prints
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
Please let me know.

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

    If you want to match the whole hostname, you will need to expand the regular expression matching its name. See perlre on what \w+ matches and also on how to include other characters in that matching.

      It worked-
      (\w+.+)
      OUPUT
      ip 10.1.1.1 goes with host name HOSTNAME-1A_1ST ip 10.1.1.2 goes with host name HOSTNAME-1B_2ND ip 10.3.1.1 goes with host name HOSTNAME-2A_1ST ip 10.3.1.2 goes with host name HOSTNAME-2B_2ND
      Thank you Corion.
        In this case your modified expression works fine because there aren't any trailing blanks after the Hostname. If that happened, something like "HOSTNAME-1A_1ST   " your expression would also pick up those extra spaces. To prevent that, you could use a character set, like ([\w\-]+) That essentially adds "-" to the \w character set.

        Update: There is more than one way to write the regex. Another possibility would be (\S+) Changing one character (\w to \S) does a lot. That would match any non-whitespace character. So if some weirdo thing like "*" showed up, it would also be included. What you choose to implement depends both upon your actual data and personal preference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1168917]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-20 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found