Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

match multi-lines from config file

by nidhi (Acolyte)
on Aug 06, 2007 at 22:34 UTC ( [id://630914]=perlquestion: print w/replies, xml ) Need Help??

nidhi has asked for the wisdom of the Perl Monks concerning the following question:

Hi all
I am trying to match a similar set of lines from a config file and store the results in a hash.

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,120,130,135,140,145,150,201 +,202,209
My approach is :
1. Find a line starting with string "interface".
2. Search the next following line with "description abc.xyz-conf"
3. Search the next following line with "switchport trunk allowed vlan 15-17,30,35,120,130,135,140,145,150,201,202,209"
4. Store the key-value pair for each descriptio-vlans found.
- Similar patterns are to be searched across the config.
Can someone suggest how do I go about doing this in perl.
Any help appreciated.

Replies are listed 'Best First'.
Re: match multi-lines from config file
by GrandFather (Saint) on Aug 06, 2007 at 22:57 UTC

    Use open to open a file handle then use:

    while (<IN>) {

    to scan the file. Use index or a regular expression match to skip (using next) until the interface line is found. Skip until the description line is found:

    /^description/ && last while <IN>; my $description = $_;

    Process the description then use the same technique for the rest of the lines you need to match.


    DWIM is Perl's answer to Gödel
      /^description/ && last while <IN>;
      that confused the heck out of me! how about this form?
      $_ = <IN> until /^description/;
      doh!

        Because that tests $_ before the first value is assigned to it and doesn't terminate for a file that doesn't include a matching line!


        DWIM is Perl's answer to Gödel
Re: match multi-lines from config file
by xicheng (Sexton) on Aug 07, 2007 at 04:28 UTC
    An easy way for this kind of problems is to reset the IRS $/, say:
    use strict; use warnings; use Data::Dumper; my %hash = (); { local $/ = "interface "; while(<DATA>) { my ($key) = /description\s+(.*)/; next if $key =~ /^\s*$/; my ($value) = /switchport trunk allowed vlan\s*(.*)/; $hash{$key} = $value; } print Dumper \%hash; __DATA__ .....
    Make sure the IRS won't show up within each block, so make it more specific accordingly...
    Regards,
    Xicheng
Re: match multi-lines from config file
by nidhi (Acolyte) on Aug 10, 2007 at 17:29 UTC
    Thanku very much all, it really helped me!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://630914]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-24 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found