I'm not quite sure from your post exactly what it is you want to do. I'm guessing that there are multiple "interface" descriptions in the configuration file and you need to extract one key/value pair from each. Processing line by line is probably not a good idea as you could get an interface with a good description line but a bad switchport line followed by the converse, thus ending up with a key from the one associated with a value from the other.

As long as the configuration file was of manageable size I would read the whole file into a string and then split it into individual interfaces using a look-ahead pattern. Then I would iterate over the interfaces extracting both key and value using a single regex and adding them to the hash.

use strict; use warnings; use Data::Dumper; my $data = do { local $/; <DATA>; }; my @interfaces = split m{(?=interface)}, $data; my $rxExtract = qr {(?xms) description\s+ (\S+) .*? switchport\strunk\sallowed\svlan\s* ([^\n]+) }; my %hash; foreach my $interface ( @interfaces ) { $hash{$1} = $2 if $interface =~ $rxExtract } print Data::Dumper->Dumpxs([\%hash], [q{*hash}]); __END__ interface Port-channel31 description abc.xyz-conf no ip address load-interval 60 switchport switchport trunk encapsulation dot1q switchport trunk allowed vlan 15-17,30,35 interface Port-channel32 description def.uvw-conf no ip address load-interval 55 switchport switchport trunk encapsulation dot1q switchport trunk allowed vlan 12-15,31-38 interface Port-channel35 description ghi.rst-conf no ip address load-interval 75 switchport switchport trunk encapsulation dot1q switchport trunk allowed vlan 21-26,44,47,51

Here's the Data::Dumper output.

%hash = ( 'ghi.rst-conf' => '21-26,44,47,51', 'def.uvw-conf' => '12-15,31-38', 'abc.xyz-conf' => '15-17,30,35' );

As I said, I'm not sure if this is what you want but, hopefully, it will give you some help.

Cheers,

JohnGG


In reply to Re: Another way to match multi-lines from config. by johngg
in thread Another way to match multi-lines from config. by nidhi

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.