in reply to Re^4: just another search program
in thread just another search program
I've posted an extended example that processes multiple Networks per file below, but you may not understand how it works, and may therefore hove problems adapting it for your real program.
Given your current level of Perl skill, I think that you are more likely to understand one of the other solutions presented in this thread, particularly grandfather's with which you have already had extensive help. I think that you should probably get yourself a good Perl book and take the time to play with a few small programs of your own before attempting more complicated tasks.
#! 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; ### This is the new code that allows it to process ### multiple networks within the same file. ### If the last interface record we read has a Network record ### appended to the end, then parse out and update the $nName +variable. $nName = $1 if m[Network\sname \s+ (\S+) \s* \nI$]x; ### If you do not understand how this works, then you should t +ry adding ### some print statements and play with this test script until + you do understand. } else { warn "File did not match required format"; } } __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: 2 135.51.05.22 321.654.987.111 Monitored circuits: vbgghdfgdfg Interface WEB_C 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: 3 135.51.05.22 123.456.789.001 321.654.987.111 Monitored circuits: vbgghdfgdfg Network name B 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: 2 135.51.05.22 321.654.987.111 Monitored circuits: vbgghdfgdfg Interface WEB_C 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: 3 135.51.05.22 123.456.789.001 321.654.987.111 Monitored circuits: vbgghdfgdfg
produces
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 B, WEB_A, 145, Master, 194.551.205.250, 1, 135.51.045.242 B, WEB_B, 145, backup, 19.51.205.20, 2, 135.51.05.22, 321.654.987.111 B, WEB_C, 145, backup, 19.51.205.20, 3, 135.51.05.22, 123.456.789.001, + 321.654.987.111
|
|---|