LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2008, 12:06 PM   #1
sal_paradise42
Member
 
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150

Rep: Reputation: 16
perl help for multiple line matching


Hello,
I have the following text that I am trying to match on:
Quote:
interface Serial1/1.1/19:0
bandwidth 1540
no ip address
encapsulation frame-relay
mls qos trust dscp
service-policy output QoS-Policy-1.54m
!
interface Serial1/1.1/19:0.1 point-to-point
ip vrf forwarding vrfData
ip address 10.136.18.1 255.255.255.252
mls qos trust dscp
frame-relay interface-dlci 69
!
interface GigabitEthernet1/13
description Data Secondary YELLOW
switchport access vlan 252
switchport mode access
!
interface GigabitEthernet1/14
description L00SLCDPM01 Data Secondary YELLOW
switchport access vlan 130
switchport mode access
!
I am only interested in the interfaces that have an IP address(like Serial1/1.1/19:0.1 above) when I use the following code and push my matches to an array
Code:
my ($lines,$hostname,@int,@ip);
        open(CONFIGURATIONS,"/var/random/ni/test") or die "couldn't open configurations";
        while (<CONFIGURATIONS>) {
                chomp;
                $lines = $_;
                if ($lines =~ /interface (\S+)(?:.*)?/) {
                        push @int,$1;
                }
                if ($lines =~ /^ (ip address ((?:\d{1,3}\.){3}\d) (?:\d{1,3}\.){3}\d)/) {
                        my $ips = $1;
                        push @ip,$ips;
                }
        }

        for (my $x=0;$x < scalar(@int) ; $x++) {
                        if (defined $ip[$x]) {
                print "$int[$x] $ip[$x]\n";
        }
                }
        close CONFIGURATIONS;
This is what happens:
Quote:
./dns.pl
Serial1/1.1/19:0 ip address 10.136.18.1 255.255.255.2
spits out the wrong interface I am expecting Serial1/1.1/19:0.1 since that IP is under it. Anyway, I was trying a new record separator since each block of interfaces seems to be separated by ! but that didn't do the trick or I may have been using it incorrectly.
Anyone?
 
Old 07-23-2008, 05:45 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
perl -e '$/ = "!"; @records=<>; foreach $record( @records ){ if( $record =~ m/ip address/ ){ print $record; }}'
Feed it your datafile by redirection.
--- rod.
 
Old 07-23-2008, 07:53 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Quote:
Originally Posted by sal_paradise42 View Post
spits out the wrong interface I am expecting Serial1/1.1/19:0.1 since that IP is under it.
Bad logic - you are presuming you always get an IP address before the next "interface" record.
 
Old 07-24-2008, 11:12 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by theNbomr View Post
Code:
perl -e '$/ = "!"; @records=<>; foreach $record( @records ){ if( $record =~ m/ip address/ ){ print $record; }}'
Feed it your datafile by redirection.
--- rod.
Beauty ... :}

Slight modification:
Code:
perl -e '$/ = "!\n"; @records=<>; foreach $record( @records ){ if( $record =~ m/^ip address/m ){ print $record; }}'
because it also matched
Quote:
Code:
interface Serial1/1.1/19:0
bandwidth 1540
no ip address
encapsulation frame-relay
mls qos trust dscp
service-policy output QoS-Policy-1.54m
!
 
Old 07-24-2008, 05:10 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Except it doesn't do what was asked. The OP apparently wants a single line of "<interface> <IP @/netmask>", but only <if IP @> is really an IP address.
IMHO (s)he is 95% there - just a small matter to resolve (instead of printing elements, print the arrays and see what you get).

Last edited by syg00; 07-24-2008 at 05:19 PM. Reason: hint added
 
Old 07-24-2008, 07:00 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Well, okay then. It does stretch the bounds of good taste for one-liners (unless you've got long lines...),but
Code:
perl -e '$/ = "!\n"; @records=<>; foreach $record( @records ){ if( $record =~ m/^ip address/m ){ @fields=split /\n/,$record;$fields[0]=~s/interface//; print "$fields[0] $fields[2]\n";}}'
--- rod.
 
Old 07-24-2008, 07:35 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by theNbomr View Post
Well, okay then. It does stretch the bounds of good taste for one-liners (unless you've got long lines...),but

Shorten by using oft-used perl default operators @_, $_ :

Code:
perl -e '$/ = "!\n"; @_=<>; foreach (@_) { if (/^ip address/m) { @f=split /\n/; $f[0]=~s/interface//; print $f[0],$f[2],"\n";}}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
perl matching script saltydog4791 Programming 4 12-05-2007 04:52 PM
perl- multiple substitutions on same line gnashley Programming 2 11-12-2007 08:29 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration