in reply to Re: just another search program
in thread just another search program

Hello

Thank you.

What if there are Multiple network names and multiple interface names; we want to get for EACH network name, there will be multiple interfaces...
The network is name A fgdfgdg interface 1a VRID 145 State: Master Primary address: 194.551.25.253 135.51.045.242 Monitored circuits: (priority 150), IBM_A (priority 150) VRID 145 State: backup Primary address: 19.51.205.20 135.51.05.22 interface B1 VRID 145 State: Master Primary address: 194.551.205.250 135.51.045.242 VRID 145 State: backup Primary address: 19.51.205.20 135.51.05.22 The network is name B fgdfgdg interface edsf_B VRID 145 State: Master Primary address: 194.551.205.250 135.51.045.242 VRID 145 State: backup Primary address: 19.51.205.20 135.51.05.22 interface Bd1 VRID 145 State: Master Primary address: 194.551.205.250 135.51.045.242 VRID 145 State: backup Primary address: 19.51.205.20 135.51.05.22 . . . The network is name BGT_ds fgdfgdg interface e_B VRID 145 dfsdfsdf sdfsdf State: Master Primary address: 194.551.205.250 135.51.045.242 sdfsdfsdf VRID 145 dfsdfsdfState: backup Primary address: 19.51.205.20 135.51.05.22 sdfsdf interface Bd1 VRID 145 sdfsdfsdf State: Master Primary address: 194.551.205.250 135.51.045.242 sdfsdf VRID 145 sdfsdf State: backup Primary address: 19.51.205.20 135.51.05.22

Replies are listed 'Best First'.
Re^3: just another search program
by GrandFather (Saint) on Feb 13, 2006 at 20:09 UTC

    Perhaps if I explain how the code works you can adapt it to your actual data? Here's the main part of code again:

    my $lastIP; while (<DATA>) { print if /network name|interface name|VRID|state|primary address/i; print "$lastIP\n\n" if /Monitored circuits:/ and defined $lastIP; $lastIP = $1 if /((\d{1,3}\.){3}\d{1,3})/; }

    $lastIP remembers the last IP address seen so it can be printed when "Monitored circuits" is noticed.

    while (<DATA>) reads the data one line at a time. You would normally use a file handled that you had created with open inFile, '<', 'filename' in place of DATA.

    print without parameters prints the contents of the default variable - in this case the data line that was last read from DATA. if /network name|i...address/i matches an interesting line (insensitive to case) and allows the print if there is a match.

    print "$lastIP\n\n" prints the IP address preceeding the line containing "Monitored circuits".

    This most important line is the one that matches "interesting lines". It does that by matching any of "network name", "interface name", "VRID", "state" or "primary address". Note the | between each string in the regular experssion pattern. They allow a match on any of the strings in the pattern.

    You should be able to take that and extend the match to do what you require. If not, show us your attempt, what it prints, and what you would like it to print. Note that we only need pertinant data and only enough to demonstrate the problem. Otherwise the problem gets burried in clutter. Reduce your data to a bare minimum the shows the issue.


    DWIM is Perl's answer to Gödel
      Hello
      #use strict; #use warnings; $/=undef; $d=<DATA>; print " Hello\n"; print if $d=~ /Device |interface |VRID|state|primary address/i; my (@ip)=($d=~m/number addresses(?:.*?)((?:\d{1,3}\.){3}\d{1,3})/gsi); print "The addresses are: ". join("\n",@ip)."\n"; __DATA__ logging to Device F01 is 19.9.16.11... Showing the vrrp status VRRP Interfaces Interface WEB_A Authentication: NoAuthentication VRID 39 State: Master Time since transition: 582693 hfghfghfghghghfghfhhfgh Primary address: 195.51.205.220 Number addresses: 1 195.51.205.222 Monitored circuits: Afgh_A (priority 1fgh50) SRghf1 (priority 1fgh50) Interface DATA_DL Nufghfghmber of vhfghl roufgh Fgfhfghs: AuthefghfgntichoAhfhication VRID 2556 State: Master Time since transition: 13405375 Bahfgh Flafghfghgs: Advertfghdl: 1 Rfghfghfgval: 3fghfghfgh fghfsgh VfghfghVRghfg Vh Primary address: 19.18.27.65 Next advertisement: 0 Number addresses: 1 15.12.57.234 I logging to Device L2 is 14.359.1.2... Showing the vrrp status VRRP Interfaces Interface SDC_A Nughujgj AuthentijghjghjNoAuthentication VRID 9 State: Backup Time since transition: 582695 Mghjghjer: hgjghjghj0 BahhjFlags: Adverghjad Interval: 3 VMAChj VMAhj1:ef Primary address: 195.51.205.221 Masghjghjg Number addresses: 1 195.51.205.222 Interface SA Numbehgjghjr of virtualj: 1 Fghjg Authentijghjtion VRID 7 State: Backup Time since transition: 582695 Master: 62.187.224.12 Baseghjghj4 Flags: Advertisement ihjghjRouterhjghj3jghjVRRP VMAC: ghj:01jghj Primary address: 62.187.224.13 Mastghjghj Number addresses: 1 6.17.26.14
      This is my code and gives the output as ip address below the "number address" but the getting the device name,interface name,VRID,State,primary address is not happening.

      any help on this ? i m missing something ?

        This may be what you want now, but really you need to sort out what problem you actually need to solve.

        Note that this is still reading the data a line at a time rather than slurping the data as your code was. Unless this code is part of something larger that needs to further process the original data there is no advantage in slurping everything. Even if you did need to process the data further, you would be better to slurp into an array than to slurp the whole thing into a scalar.

        use strict; use warnings; my $dumpIPs = 0; while (<DATA>) { if ($dumpIPs and /((\d{1,3}\.){3}\d{1,3})/) { print "$1 "; } elsif ($dumpIPs) { print "\n\n"; $dumpIPs = 0; } if (/\b(?:Device|interface|VRID|state|primary address)\b/i) { print; $dumpIPs = 0; } $dumpIPs ||= /Number addresses/; }

        Prints:

        logging to Device F01 is 19.9.16.11... Interface WEB_A VRID 39 State: Master Time since transition: Primary address: 195.51.205.220 195.51.205.222 Interface DATA_DL VRID 2556 State: Master Time since transition: Primary address: 19.18.27.65 15.12.57.234 logging to Device L2 is 14.359.1.2... Interface SDC_A VRID 9 State: Backup Time since transition: Primary address: 195.51.205.221 195.51.205.222 Interface SA VRID 7 State: Backup Time since transition: Primary address: 62.187.224.13 6.17.26.14

        DWIM is Perl's answer to Gödel
      Hello

      What if i have to take input from a text file and print the output to a excel file for the same output we have above ?