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

Hello monks
The text file contents are as follows :
Network name A Interface WEB_A ggggggggg VRID 145 State: Master XXXXXXXXXXXX Effective Priority: 200 07:09:56:04:0t:ef Primary address: 194.551.205.250 Number addresses: 1 135.51.045.242 Monitored circuits: vbgghdfgdfg Interface WEB_B ggggggggg Agggggg VRID 145 State: backup XXXXXXXXXXXX Effective Priority: 200 07:09:56:04:0t:ef Primary address: 19.51.205.20 Next advertisement: 0 Number addresses: 1 135.51.05.22 Monitored circuits: vbgghdfgdfg
This is the output of a text file. i need to pull only the
the network name,interface name,VRID,state,primary address and the address above Monitored circuit. since these are ip address i m confused about the matching ip address too.

Any suggestions ?

Replies are listed 'Best First'.
Re: just another search program
by GrandFather (Saint) on Feb 11, 2006 at 10:25 UTC

    What have you tried? The following may be what you want:

    use strict; use warnings; 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})/; }

    Prints:

    Network name A 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

    DWIM is Perl's answer to Gödel
      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

        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
Re: just another search program
by vladb (Vicar) on Feb 11, 2006 at 10:34 UTC
    Another way to grab all other ip addresses (preceding the "Monitored ..." line) is with the pattern match like this one:
    $/=undef; $d=<DATA>; my (@ip)=($d=~m/number addresses(?:.*?)((?:\d{1,3}\.){3}\d{1,3})/gsi); print "Other addresses: ". join("\n",@ip)."\n"; __DATA__ Network name A Interface WEB_A ggggggggg VRID 145 State: Master XXXXXXXXXXXX Effective Priority: 200 07:09:56:04:0t:ef Primary address: 194.551.205.250 Number addresses: 1 135.51.045.242 Monitored circuits: vbgghdfgdfg Interface WEB_B ggggggggg Agggggg VRID 145 State: backup XXXXXXXXXXXX Effective Priority: 200 07:09:56:04:0t:ef Primary address: 19.51.205.20 Next advertisement: 0 Number addresses: 1 135.51.05.22 Monitored circuits: vbgghdfgdfg


    _____________________
    "We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
    the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true."

    Robert Wilensky, University of California

Re: just another search program
by BrowserUk (Patriarch) on Feb 11, 2006 at 16:15 UTC

    It appears from the example that the 'Network name' is a header, and are multiple 'Interface records', but it's not clear whether there could be multiple Network Name/Interface groups per file? The following assumes not.

    It would also appear that could be more than one "address above Monitored circuit.", going by the "Number of addresses" field. I don't think either of the other solutions would cater for that? If so, this might be useful. I've also captured and output the Number of IPs value, but that is easily removed or discarded.

    You'll note that this makes no attempt to verify the format or legality of the ip addresses whilst parsing the file, simply treating them as whitespace delimited tokens. If you need to verify the addresses, that is better done as a separate step.

    #! perl -slw use strict; my( $nName, $iName, $vrid, $state, $paddr, $nAddr, $oAddrs, @oAddrs ); die "File does not contain a Network name header" unless ( $nName ) = <DATA> =~ m[Network\sname \s+ (\S+)]x; local $/ = "\nI"; while( <DATA> ) { if( ( $iName, $vrid, $state, $paddr, $nAddr, $oAddrs ) = m[ (?=.*? nterface \s+ (\S+) ) (?=.*? VRID \s+ (\S+) ) (?=.*? State: \s+ (\S+) ) (?=.*? Primary\saddress: \s+ (\S+) ) (?=.*? Number\saddresses: \s+ (\S+) ( .+? ) (?=Monitored\scircuits:) ) ]smx ) { @oAddrs = split ' ', $oAddrs; print join ', ', $nName, $iName, $vrid, $state, $paddr, $nAddr +, @oAddrs; } else { warn "File did not match required format"; } }

    Output

    C:\test>junk1 A, WEB_A, 145, Master, 194.551.205.250, 1, 135.51.045.242 A, WEB_B, 145, backup, 19.51.205.20, 2, 135.51.05.22, 321.654.987.111 A, WEB_C, 145, backup, 19.51.205.20, 3, 135.51.05.22, 123.456.789.001, + 321.654.987.111

    Extended dataset


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi

      when there is multiple network the code does print each of the network name ?

      Any clues ?

        Maybe you missed the bit where I said

        but it's not clear whether there could be multiple Network Name/Interface groups per file? The following assumes not.

        If there are multiple 'Network name'/'Interface name' groups in each file, then you could adapt my solution with an extra outer loop, but you would probably be better to use one of the other solutions.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.