in reply to Parsing multiple lines based on a given String

Your 2 line example is not quite enough to write some code for this.

Some general advice: It is almost always easier to write "look ahead" rather than "look behind" code. By that I mean that parsing a file like this, I would find the HOSTNAME first and the IP address second. I associate those 2 things together and then decide to keep it or not. This is easier than finding the IP address and then trying to figure out in the previous lines what HOSTNAME it goes with.

Are you able to make a stab at some Perl code?
Just a few more example lines would be helpful.

  • Comment on Re: Parsing multiple lines based on a given String

Replies are listed 'Best First'.
Re^2: Parsing multiple lines based on a given String
by ArifS (Beadle) on Jun 22, 2016 at 16:10 UTC
    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 .......
      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.
      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,
      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);