llarochelle has asked for the wisdom of the Perl Monks concerning the following question:
I've been trying to create a config file parser to parse Cisco IOS configs and such. The final objective would be to show relevant data in contexts based on filters in a configuration file. For example, with such a config file it would display all interfaces where we've found the line "access vlan" as a child of the "interface" context and only show lines containing "speed", "duplex" and "description".
{ 'Context' => '^interface', 'Types' => [ 'Switch', ], 'Condition' => 'access vlan', 'Filter' => [ 'speed', 'duplex', 'description' ] };
So far, so good. I read the "running-config" and I index the lines depth (given that a non-empty line , not beginning with a space (\s) has a depth of 0) in an array. Then, in another read I use that index to read the data again, this time using relative position based on depth to create the "childs" of a context. Here's the function :
sub getDeep { my @data = (@_); my ($bighash,$hash); #First read foreach my $idx (0.. $#data) { my ($spaces, $content) = ($data[$idx] =~ m/^(\s*) +(.*)/); my $depth = length $spaces; $bighash->{node}{$idx}{depth} = $depth; } # Variables for the first read my $ldepth = 0; my $lcontext; my $lid; # Second read foreach my $id (0 .. $#data) { $data[$id] =~ s/^\s*//; next if ($data[$id] =~ /^!/); my $depth = $bighash->{node}{$id}{depth}; if ($depth eq 0) { push (@{$hash->{global}} , $data[$ +id]); $lcontext = $data[$id]; $lid = $id; } if (($depth gt 0) && ($id - $lid eq 1)) { push (@{$hash->{$lcontext}}, (" " +x $depth. $data[$id])); $lid = $id; } } return $hash; }
Using this sub, I can return a hash, then based on the presence of an arrayref for a given key, apply filters as explained. This works pretty well, so far very proud of this piece of code. Problem comes when I want to find childs of childs. In the example below, the childs of "given param2" would reprensent my next challenge.
interface XYZ given param1 -> child of "interface XYZ" given param2 -> child of "interface XYZ" given param2.1 -> child of "given param2" given param2.2 -> child of "given param2" given param3 -> child of "interface XYZ"
So after thinking about this for a while and failing with different approaches, my question comes in 2 separate parts :
1) Is there a better way to do this that I'm not seeing ?
2) How could I keep tagging childs of childs as the lines dig deeper and identify them properly in a data structure ?
Thank you for reading up to this line :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parsing by indentation
by tybalt89 (Monsignor) on Oct 24, 2018 at 19:30 UTC | |
by llarochelle (Beadle) on Oct 24, 2018 at 20:10 UTC | |
by tybalt89 (Monsignor) on Oct 24, 2018 at 20:49 UTC | |
by llarochelle (Beadle) on Oct 25, 2018 at 02:14 UTC | |
by llarochelle (Beadle) on Oct 25, 2018 at 14:13 UTC | |
by hippo (Archbishop) on Oct 25, 2018 at 15:16 UTC | |
| |
by AnomalousMonk (Archbishop) on Oct 25, 2018 at 17:02 UTC | |
by LanX (Saint) on Oct 27, 2018 at 16:21 UTC | |
by llarochelle (Beadle) on Oct 29, 2018 at 12:56 UTC | |
Re: Parsing by indentation
by atcroft (Abbot) on Oct 24, 2018 at 20:18 UTC | |
Re: Parsing by indentation
by LanX (Saint) on Oct 25, 2018 at 04:03 UTC | |
by LanX (Saint) on Oct 25, 2018 at 15:12 UTC | |
Re: Parsing by indentation
by Corion (Patriarch) on Oct 24, 2018 at 18:25 UTC | |
by llarochelle (Beadle) on Oct 24, 2018 at 19:32 UTC | |
by stevieb (Canon) on Oct 24, 2018 at 20:14 UTC | |
by llarochelle (Beadle) on Oct 24, 2018 at 20:33 UTC | |
by stevieb (Canon) on Oct 24, 2018 at 20:39 UTC | |
| |
Re: Parsing by indentation
by karlgoethebier (Abbot) on Oct 27, 2018 at 14:15 UTC | |
by llarochelle (Beadle) on Oct 29, 2018 at 15:00 UTC |