This should give you a clean start.

I suppose you'll be able to adjust the methods to your needs.

Especially ->__out__ where you can write the new format based on the already parsed data.

Based on the limited insight I have, I tried to keep it as generic as possible.

YMMV but HTH! :)

# https://perlmonks.org/index.pl?node_id=11132953 use strict; use warnings; use Data::Dump qw/pp dd/; my $obj = "CiscoService"; while (<DATA>) { chomp; my ($indent,$data) = /^(\s*)(.*)$/; my ($call, @args) = split/\s+/, $data; $call =~ s/^!$/__out__/; $call =~ s/-/_/g; if ( my $meth = $obj->can($call) ) { $obj = $meth->( $obj, length($indent),@args) } else { warn "<$call> not impemented"; } } package CiscoService; use Data::Dump qw/pp dd/; sub service { my ($self,$indent,@args) = @_; return bless {__head__ => \@args, sub=>{} },__PACKAGE__; } sub description { my ($self,$indent,@args) = @_; $self->{sub}{description} = \@args; return $self; } sub service_policy { my ($self,$indent,@args) = @_; $self->{sub}{"service-policy"} = \@args; return $self; } sub encapsulation { my ($self,$indent,@args) = @_; $self->{sub}{encapsulation} = \@args; return $self; } sub rewrite { my ($self,$indent,@args) = @_; $self->{sub}{rewrite} = \@args; return $self; } sub xconnect { my ($self,$indent,@args) = @_; $self->{sub}{xconnect} = {head =>\@args, sub=>{}}; return $self; } sub mtu { my ($self,$indent,@args) = @_; $self->{sub}{xconnect}{sub}{mtu} = \@args; return $self; } sub __out__ { my ($self,$indent,@args) = @_; pp $self; return "CiscoService"; } package main; __DATA__ service instance 20 ethernet description Internal site encapsulation dot1q 17 rewrite ingress tag pop 1 symmetric xconnect 192.168.51.20 838969735 encapsulation mpls mtu 9202 ! service instance 21 ethernet description Client 1 important encapsulation dot1q 18 rewrite ingress tag pop 1 symmetric xconnect 192.168.51.21 838969736 encapsulation mpls mtu 9202 ! service instance 22 ethernet description Client other 2 encapsulation dot1q 22 rewrite ingress tag pop 1 symmetric xconnect 192.168.56.22 838925386 encapsulation mpls mtu 9202 ! service instance 23 ethernet description Client name with fun stuff encapsulation dot1q 3200 second-dot1q 5 rewrite ingress tag pop 2 symmetric service-policy input CoS_222_333_23 xconnect 192.168.56.23 571125465 encapsulation mpls mtu 9202 ! service instance 24 ethernet description Client - not as important encapsulation dot1q 3200 second-dot1q 6 rewrite ingress tag pop 2 symmetric service-policy input 222_444_24 xconnect 192.168.56.24 571637077 encapsulation mpls mtu 9202 !

bless({ __head__ => ["instance", 20, "ethernet"], sub => { description => ["Internal", "site"], encapsulation => ["dot1q", 17], rewrite => ["ingress", "tag", "pop", 1, "symmetric"], xconnect => { head => ["192.168.51.20", 838969735, "encapsula +tion", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 21, "ethernet"], sub => { description => ["Client", 1, "important"], encapsulation => ["dot1q", 18], rewrite => ["ingress", "tag", "pop", 1, "symmetric"], xconnect => { head => ["192.168.51.21", 838969736, "encapsula +tion", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 22, "ethernet"], sub => { description => ["Client", "other", 2], encapsulation => ["dot1q", 22], rewrite => ["ingress", "tag", "pop", 1, "symmetric"], xconnect => { head => ["192.168.56.22", 838925386, "encapsula +tion", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 23, "ethernet"], sub => { "description" => ["Client", "name", "with", "fun", "stuff"], "encapsulation" => ["dot1q", 3200, "second-dot1q", 5], "rewrite" => ["ingress", "tag", "pop", 2, "symmetric"], "service-policy" => ["input", "CoS_222_333_23"], "xconnect" => { head => ["192.168.56.23", 571125465, "encaps +ulation", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 24, "ethernet"], sub => { "description" => ["Client", "-", "not", "as", "important"], "encapsulation" => ["dot1q", 3200, "second-dot1q", 6], "rewrite" => ["ingress", "tag", "pop", 2, "symmetric"], "service-policy" => ["input", "222_444_24"], "xconnect" => { head => ["192.168.56.24", 571637077, "encaps +ulation", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService")

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re: Cisco to Juniper - parser help by LanX
in thread Cisco to Juniper - parser help by jamescmatt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.