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

Perl Monks: I have to extract each encryption key information from the following config file output.
show run Building configuration... ! encryption key 1 size 40bit 7 621AA6093A25 transmit-key encryption key 2 size 40bit 7 7260637E4720 encryption key 3 size 40bit 7 0F733F116035 encryption key 4 size 40bit 7 03B71F3D601D encryption mode wep mandatory !
I need some information like the following for each encryption keys.
key : 1 size: 40 value: 621AA6093A25 (the length could be different) TransmitKey: true # The number 7 in the encryption key information should be ignored.
Currently using the code below I able to extract text like "size 40bit 7 621AA6093A25 transmit-key", that is the one's following "encryption key x ".
for ($Count=1; $Count<=4; $Count++) { if ($showrunOutput=~/encryption\skey\s$Count\s(.+)\s\n/){ print $1; } }
Please help me in extracting the required information Thanks!

Replies are listed 'Best First'.
Re: Extracting specific text info from the config file ouput
by philcrow (Priest) on Dec 16, 2005 at 21:41 UTC
    I would probably just split on space and keeps the bits I needed:
    my ( undef, undef, $key_num, undef, $size, undef, $key ) = split /\s+/, $input;

    There's probably something prettier, but I think that should work.

    Phil

Re: Extracting specific text info from the config file ouput
by InfiniteSilence (Curate) on Dec 16, 2005 at 21:45 UTC
    If you insist on using a regex:
    #!/usr/bin/perl -w use strict; while(<DATA>){ if(/\s+encryption key (\d+) size (\d+)bit \d+ (\S+)/){ print qq|$1\t$2\t$3\n|; } } 1; __DATA__ show run Building configuration... ! encryption key 1 size 40bit 7 621AA6093A25 transmit-key encryption key 2 size 40bit 7 7260637E4720 encryption key 3 size 40bit 7 0F733F116035 encryption key 4 size 40bit 7 03B71F3D601D encryption mode wep mandatory !

    Celebrate Intellectual Diversity

      Great! The regular expression works fine for me. Thank you for the help!
      I have a question which is an extension of the solution that I got for my previous problem. Extracting specific text info from the config file ouput
      I have a string as specified in the code below. I need a mechanisam to extract the authentication information.
      For me the right authentication information are
      1. authentication open <and/or followed by something>
      eg1: authentication open network-eap
      eg2: authentication open
      2. authentication shared <and/or followed by something>
      That means I should pick only the ones that start with authentication. Not the one starting with 'aaa' in the example below.
      Also when I am looping I should strip of the part which I extracted from the original string.

      Could some one tell me how to do it?
      my $str = "aaa authentication login\x0d\n ssid Rich\x0d\n authentic +ation open\x0d\n authentication shared \x0d\n"; my $authentication; while ($str =~/authentication (\S+)/){ $authentication = $authentiction. $1. " "; } # At the end of the while loop # $authentication should have the value # $authentication = open shared
      Any input provided will be much appreciated! Thanks.