the correct way would be to form a small grammer and parse it. there is an article on
using Parse::RecDescent for parsing.
however this seems really trivial. you can handcode it easily.
for example something like
sub tokenize
{
until (defined ($token = shift (@tokens)) {
return
unless (defined ($line = <MIB>));
next if ($line =~ /$comment/);
while ($line =~ /([a-zA-Z0-9]+|OBJECT|IDENTIFIER|::=|[{}])/g) {
####### NOT TESTED #######
push (@tokens, $1);
}
}
return ($token);
}
could be used to carve the data stream into tokens (reading it a line at a time).
calling this
will return a token at a time till EOF. you can use it thus.
while (defined ($token = tokenize ())) {
# expect some data
# expect OBJECT, IDENTIFIER, ::=
# expect opening brace
# expect data followed by number
# expect closing brace
#do something with the data
}
/kk
On Tue, Aug 21, 2001 at 02:25:05PM -0400, Craig Moynes/Markham/IBM wrote:
> Hi all,
>
> I have been tasked with reading and parsing a large MIB file. I
> eventually have to create the traps but that's beyond this post.
>
> Currently I am looking for a certain line of text, and for the most part
> what I have written works. Looking for lines similar to this:
>
> dot3ChipSetAMD79C940 OBJECT IDENTIFIER ::= { dot3ChipSetAMD 3 }
> dot3ChipSetIntel OBJECT IDENTIFIER ::= { dot3ChipSets 2 }
> dot3ChipSetIntel82586 OBJECT IDENTIFIER ::= { dot3ChipSetIntel 1 22
> 4 5}
> dot3ChipSetIntel82596 OBJECT IDENTIFIER ::= { dot3ChipSetIntel 2 }
>
> But occasionally I get something like this:
> dot3ChipSetIntel82596 OBJECT IDENTIFIER ::=
> { dot3ChipSetIntel 2 }
>
> 'or'
>
> dot3ChipSetIntel82596
> OBJECT IDENTIFIER ::=
> { dot3ChipSetIntel 2 }
>
> So I need to search multiple lines for a string...but I cannot slurp the
> whole file into memory ( Its currently over 12 megs), and I don't know
> where the carriage return will be located.
>
> I have several solutions in mind, one is using an queue (LIFO) and put new
> lines read in so I would always have say the current line and the next line
> in the list...then if I cant match I can concatenate the two lines together
> and search that.
>
> here is the test script I am currently using:
> #!/usr/bin/perl -w
> use strict;
>
> select ( STDOUT );
> $| = 1;
>
> my $wRX = "\[\\w\\-\]";
> my $commentRX = qr/^--/;
>
> my $objectIdentifierRX = qr/^\s*($wRX+)\s+OBJECT\s+IDENTIFIER\s+::
> =\s+{\s+($wRX+)\s+(\d+)\s*(\s+(\d+))+\s*}\s*$/;
>
> open( MIB, "<master.mib") or die("Could not open master.mib\n");
> print "- $objectIdentifierRX\n";
> while( <MIB> )
> {
> my ($objectName, $objectParent, @objectIDs);
>
> next if ( $_ =~ /$commentRX/ );
> if ( $_ =~ m/$objectIdentifierRX/ )
> {
> print "$_";
> }
> elsif ( $_ =~ /OBJECT\s+IDENTIFIER/ )
> {
> print "**** $_";
> sleep 1;
> }
> }
>
>
>
> You can grab some sample mibs at:
> ftp.cisco.com/pub/mibs/v1
>
> Just cat them together to form a larger MIB.
>
> Thanks.
>
> -----------------------------------------
> Craig Moynes
> IBM Global Services, Canada
> [EMAIL PROTECTED]
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]