For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Help parsing a txt by blocks...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help parsing a txt by blocks...
Juan Pablo Feria Gomez

2006-10-04, 9:57 pm

I want to parse a big cisco configuration on TXT and create arrays per
each configuration block...

The script will know when each section begins/end with the "!" character

I'm stuck.. :(

Can someone give me any pointers on the documentation?

Thanks in advance...

Here is a fragment of a configuration..

!
dial-peer voice 3500 voip
description potx
destination-pattern 35..
session target ipv4:1.79.15.74
codec g729r8 bytes 50
ip qos dscp cs3 media
ip qos dscp cs3 signaling
!
dial-peer voice 4600 voip
description poty
destination-pattern 46..
session target ipv4:3.25.114.190
codec g729r8 bytes 50
ip qos dscp cs3 media
ip qos dscp cs3 signaling
!
DJ Stunks

2006-10-04, 9:57 pm

On Oct 4, 7:44 pm, jferi...@gmail.com (Juan Pablo Feria Gomez) wrote:
> I want to parse a big cisco configuration on TXT and create arrays per
> each configuration block...
>
> The script will know when each section begins/end with the "!" character
>
> I'm stuck.. :(
>
> Can someone give me any pointers on the documentation?
>
> Thanks in advance...
>
> Here is a fragment of a configuration..
>
> !
> dial-peer voice 3500 voip
> description potx
> destination-pattern 35..
> session target ipv4:1.79.15.74
> codec g729r8 bytes 50
> ip qos dscp cs3 media
> ip qos dscp cs3 signaling
> !
> dial-peer voice 4600 voip
> description poty
> destination-pattern 46..
> session target ipv4:3.25.114.190
> codec g729r8 bytes 50
> ip qos dscp cs3 media
> ip qos dscp cs3 signaling
> !


You will want to look at the doc page 'perlvar', specifically
the $/ variable - in English, the $INPUT_RECORD_SEPARATOR.

Here's a framework to get you started.

-jp

#!/usr/bin/perl

use strict;
use warnings;

use English qw{ -no_match_vars };
$INPUT_RECORD_SEPARATOR = '!';

while ( my $record = <DATA> ) {
print "[$record]\n";
}

__DATA__
!
dial-peer voice 3500 voip
description potx
destination-pattern 35..
session target ipv4:1.79.15.74
codec g729r8 bytes 50
ip qos dscp cs3 media
ip qos dscp cs3 signaling
!
dial-peer voice 4600 voip
description poty
destination-pattern 46..
session target ipv4:3.25.114.190
codec g729r8 bytes 50
ip qos dscp cs3 media
ip qos dscp cs3 signaling
!

__END__

John W. Krahn

2006-10-05, 3:58 am

Juan Pablo Feria Gomez wrote:
> I want to parse a big cisco configuration on TXT and create arrays per
> each configuration block...


It looks easy enough.

> The script will know when each section begins/end with the "!" character


There are probably a few ways you could handle that.

> I'm stuck.. :(


On what, in particular?

> Can someone give me any pointers on the documentation?


Pointers to what?

> Here is a fragment of a configuration..
>
> !
> dial-peer voice 3500 voip
> description potx
> destination-pattern 35..
> session target ipv4:1.79.15.74
> codec g729r8 bytes 50
> ip qos dscp cs3 media
> ip qos dscp cs3 signaling
> !
> dial-peer voice 4600 voip
> description poty
> destination-pattern 46..
> session target ipv4:3.25.114.190
> codec g729r8 bytes 50
> ip qos dscp cs3 media
> ip qos dscp cs3 signaling
> !


This may give you some ideas (UNTESTED):

use warnings;
use strict;

my $file = 'big cisco configuration on TXT';
open my $fh, '<', $file or die "Cannot open '$file' $!";

my @data;
$/ = "!\n"; # set Input Record Separator
while ( <$fh> ) {
next unless /codec\s+(\S+)\s+bytes\s+(\d+)/;
push @data, [ $1, $2 ];
}

# etc., etc.



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com