Home: Perl Programming Help: Regular Expressions:
Help. Get words between commas... please help!!!



Alvaro
Novice

Feb 25, 2010, 2:20 PM


Views: 12448
Help. Get words between commas... please help!!!

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


Views: 12421
Re: [Alvaro] Help. Get words between commas... please help!!!

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


Views: 12418
Re: [shawnhcorey] Help. Get words between commas... please help!!!

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


Views: 12288
Re: [Alvaro] Help. Get words between commas... please help!!!

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


Views: 12287
Re: [rovf] Help. Get words between commas... please help!!!

Guys,

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

Best regards,

Alvaro