Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Parsing multiple lines based on a given String

by ArifS (Beadle)
on Jun 22, 2016 at 16:10 UTC ( [id://1166277]=note: print w/replies, xml ) Need Help??


in reply to Re: Parsing multiple lines based on a given String
in thread Parsing multiple lines based on a given String

I can print the 2 lines that I am looking for....

Original content restored by GrandFather below

here is what I tried-
file.txt -------------- object-group network HOSTNAME_1ST network-object host 10.1.1.1 object-group service WEB_TCP tcp port-object eq 80 --------------
Trying to get the 1st 2 lines while matching an ip address.
use strict; use warnings; my $iphostname; my $filename = 'file.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; for $iphostname ($fh) { print $iphostname if (/^object-group/ ... /(\d{1,3})\.(\d{1,3})\.(\d +{1,3})\.(\d{1,3})$/); }
it gives me an error - Use of uninitialized value $_ in pattern match (m//) at .......

Replies are listed 'Best First'.
Re^3: Parsing multiple lines based on a given String
by choroba (Cardinal) on Jun 22, 2016 at 16:21 UTC
    To read from a file handle, use the appropriate operator. To match a variable against a regex, you need to tell Perl what variable to match:
    while (my $iphostname = <$fh>) { print $iphostname if $iphostname =~ /^object-group/ .. $iphostname + =~ /.../;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thank you. That helped.
Re^3: Parsing multiple lines based on a given String
by Marshall (Canon) on Jun 22, 2016 at 17:20 UTC
    What choroba is showing is called the "flip flop" operator. Have a look at this Flipin good, or a total flop? in the tutorials section.

    Other techniques could possibly be applicable, but I don't know enough about what you are doing to make a suggestion. As always, showing some actual code that you are having trouble with is the best way to get quality advice.

      At the time I was replying, the parent node contained a code sample, something like
      for $iphostname ($fh) { print $iphostname if /^object-group/ .. /\d{4}:\d{4,6}:\d{3}/; }

      Someone has edited their node without notification. They should check How do I change/delete my post?.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        It appears that the original content has been restored by grandfather. This is a real mess when the question changes without a clear update posting. I didn't have any idea of where this came from!
Re^3: Parsing multiple lines based on a given String
by Marshall (Canon) on Jun 22, 2016 at 22:07 UTC
    There are a number of ways to implement this code.
    One is shown below.
    Basically, keep track of the last hostname found. When you find a valid ip, and that ip is the one that you are looking for, then that goes with the most previous hostname.
    #!usr/bin/perl use warnings; use strict; my $file =<<END; object-group network HOSTNAME_1ST network-object host 10.1.1.1 object-group network HOSTNAME_2nd network-object host 10.3.1.1 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) and $ip eq '10.3.1.1') { print "ip $ip goes with host name $hostname\n"; } } __END__ ip 10.3.1.1 goes with host name HOSTNAME_2nd
      I tried to replace the following line-
      open my $fh, '<', \$file or die "unable to open read file $!";
      with -
      my $filename = 'file.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
      and it doesn't give an output or any error. Any clue? --------------------------------------------------------------- oh, never mind... had to use chomp. chomp($line);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found