CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
parsing out variables from multiple lines

 



wfsteadman
Novice

Feb 20, 2017, 11:18 AM

Post #1 of 2 (1250 views)
parsing out variables from multiple lines Can't Post

Greetings all,
I am trying to loop through (I think that is what I want to do) a text file with the following format:


Code
edit port3.59 
set vdom trust
unset allowaccess
set mode static
set ip 1.1.1.1 255.255.255.248
set interface port3
set vlanid 59
set status up
next
edit port5.120
set vdom trust
set allowaccess ping
set mode static
set ip 2.2.2.2 255.255.255.248
set interface port5
set vlanid 120
set status up
next
edit port5.141
set vdom trust
set allowaccess ping
set mode static
set ip 3.3.3.3 255.255.255.248
set interface port5
set vlanid 141
set status up
next


I am trying to get it so I can pull certain information. I need to pull out the port details so that would be the edit line, then I need to pull out the set ip details

so I would want the ouput to be in the following format

port359, 1.1.1.1 255.255.255.248
port5.120, 2.2.2.2 255.255.255.248
port5.141, 3.3.3.3 255.255.255.248

I see that each one is in a set block patter that starts with edit and ends with next but not sure how to pull that into some format where I can then parse through it and grab what is needed. Any assistance would be apprecited.

Thanks
Wally


BillKSmith
Veteran

Feb 20, 2017, 2:00 PM

Post #2 of 2 (1244 views)
Re: [wfsteadman] parsing out variables from multiple lines [In reply to] Can't Post

Very little Perl code is required. Use special variables (ref: perldoc perlvar) to specify input and output formats. Use regular expressions to extract the data. (Use the module Regexp::Common in the regex for extracting the ip addresses. You should consider using the perl runtime flag -n (ref perldoc perlrun).




Code
use strict; 
use warnings;
use Regexp::Common qw /net/;
use English;
$INPUT_RECORD_SEPARATOR = " next\n";
$OUTPUT_RECORD_SEPARATOR = "\n";
$OUTPUT_FIELD_SEPARATOR = ', ';
my $IP = qr/$RE{net}{IPv4}/;
while (<>) {
my ($port) = m/^\s?edit\s+(port\d\.\d{1,3})/ ;
my ($ip) = m/^\s*set\sip\s ( $IP \s+ $IP ) /xms;
print $port, $ip;
}

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives