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: Regular Expressions:
Help. Get words between commas... please help!!!

 



Alvaro
Novice

Feb 25, 2010, 2:20 PM

Post #1 of 5 (5353 views)
Help. Get words between commas... please help!!! Can't Post

Hi,

I need to grab specific parts of a configuration file and copy them to another .txt file.

A sample of the configuration:

===
interface gigabitEthernet 3/0/0.1445
vlan id 1445
ip description IRA 1934962; RJIRCS_EHI-101; HUAWEI; IRA-RJ-MAN-SWTL3-J01; ge-3/0/5.1445
pppoe
===

What I need is the word between the first and second ";". This word always changes, so I need to check the line that starts with "ip description" (there's always a space before it) and grab it to a file. There could be several commands like that on one file, so I need to append.

As I'm a telecommunication guy, Perl is really not my expertise, so I just got the scipt below, to verify I was reading line by line. That's what I got so far.

#!C:\Perl\bin\perl.exe
use English;

open (FILE, "<BRAS.txt");

while (<FILE>)
{

my($line) = $_;
chomp($line);

print "$line\n";
}

close (FILE);

Can anyone help, PLEASE!!!!

Thanks so much


shawnhcorey
Enthusiast


Feb 26, 2010, 6:07 AM

Post #2 of 5 (5326 views)
Re: [Alvaro] Help. Get words between commas... please help!!! [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

while( <DATA> ){
if( my ( $word ) = $_ =~ m{ \A \s* ip \s+ description \b [^;]* \; ([^;]+) }msx ){
print "$word\n";
}
}

__DATA__
interface gigabitEthernet 3/0/0.1445
vlan id 1445
ip description IRA 1934962; RJIRCS_EHI-101; HUAWEI; IRA-RJ-MAN-SWTL3-J01; ge-3/0/5.1445
pppoe


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Alvaro
Novice

Feb 26, 2010, 6:43 AM

Post #3 of 5 (5323 views)
Re: [shawnhcorey] Help. Get words between commas... please help!!! [In reply to] Can't Post

Shawn,

Will try it, but seems to work.

Thanks so much for your time and help!

Regards,

Alvaro


rovf
Veteran

Mar 5, 2010, 5:11 AM

Post #4 of 5 (5193 views)
Re: [Alvaro] Help. Get words between commas... please help!!! [In reply to] Can't Post

An alternative solution would be to split each line into individual fields:


Code
my @fields=split(/\s*;\s'/,$line); 
if(@fields && $fields[0] eq '....') { .... }



Alvaro
Novice

Mar 5, 2010, 5:14 AM

Post #5 of 5 (5192 views)
Re: [rovf] Help. Get words between commas... please help!!! [In reply to] Can't Post

Guys,

It worked perfectly. Thanks so much for your time and help!

Best regards,

Alvaro

 
 


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

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