Regular Expression help!
Hi All,
I need help in matching the regular expression, the file is as below.
I am trying to match number followed by Number ex 587, 128 in $1 and
60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 in $2
the $1 match works find with regulare expression #if ($_=~
/\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
However i am not sure how to get both $1 and $2 togather.
Ideally i would like to have an output printed
print "$1 wwn is $2 \n";
Any help on this would be much appreciated.
FILE
-----------------------------------------------
LOGICAL UNIT NUMBER 587
UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11
LOGICAL UNIT NUMBER 128
UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11
LOGICAL UNIT NUMBER 763
UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11
-----------------------------------------------------------
#!/usr/bin/perl
open(FILE, "clrlist") || die "Can't open clrlist : $!\n";
while (<FILE>) {
#if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})\n[a-z0-9+]\s*(\d{2}*\d{2}*\d {2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d {2}*\d{2}*\d{2}*\d{2}*)/i)
{
print "$1 ";
print "$2";
}
}
---------------------
Sj
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular Expression help!
I ended up confused after reading your email.
Please specify INPUT + OUTPUT/condition.
You have already specify INPUT which is:
LOGICAL UNIT NUMBER 587
UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11
LOGICAL UNIT NUMBER 128
UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11
LOGICAL UNIT NUMBER 763
UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11
What is the desired OUTPUT/condition?
Is it something like this?
587 wwn is 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11
128 wwn is 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11
763 wwn is 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11
On Wed, May 11, 2011 at 8:38 AM, jet speed <speedjet5 [at] googlemail.com> wrote=
:
> Hi All,
>
> I need help in matching the regular expression, the file is as below.
>
> I am trying to match number followed by Number ex 587, 128 in $1 and
> 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 in $2
>
> the $1 match works find with regulare expression =A0#if ($_=3D~
> /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
>
> However i am not sure how to get both $1 and $2 togather.
>
> Ideally i would like to have an output printed
>
> print "$1 wwn is $2 \n";
>
> Any help on this would be much appreciated.
>
>
> FILE
> -----------------------------------------------
>
>
> LOGICAL UNIT NUMBER 587
> UID: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A060:06:01:60:42:40:21:=
00:3A:AA:55:37:91:8A:DF:11
>
> LOGICAL UNIT NUMBER 128
> UID: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A060:06:01:60:50:40:21:=
00:D0:23:D5:C2:BA:D9:DE:11
>
> LOGICAL UNIT NUMBER 763
> UID: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A060:06:01:60:50:40:21:=
00:25:C6:3F:A7:CA:2D:DF:11
>
> -----------------------------------------------------------
>
>
> #!/usr/bin/perl
> open(FILE, "clrlist") || die "Can't open clrlist : $!\n";
>
> while (<FILE>) {
> #if ($_=3D~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
> if ($_=3D~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})\n[a-z0-9+]\s*(\d{2}*\d{2}*\d {2=
}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2 }*\d{2}*\d{2}*\=
d{2}*)/i)
> {
> print "$1 ";
> print "$2";
> =A0 =A0 =A0 =A0}
> }
>
> ---------------------
>
> Sj
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular Expression help!
On 11-05-11 11:38 AM, jet speed wrote:
> I need help in matching the regular expression, the file is as below.
>
> I am trying to match number followed by Number ex 587, 128 in $1 and
> 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 in $2
>
> the $1 match works find with regulare expression #if ($_=~
> /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
>
> However i am not sure how to get both $1 and $2 togather.
>
> Ideally i would like to have an output printed
>
> print "$1 wwn is $2 \n";
>
> Any help on this would be much appreciated.
You are trying to do too much in one regular expression. Process the
file one line at a time.
#!/usr/bin/env perl
use strict;
use warnings;
my $lun = 0;
while( <DATA> ){
if( /^\s*LOGICAL\s+UNIT\s+NUMBER\s+(\d+)/ ){
$lun = $1;
}elsif( /^UID:\s*(\S+)/ ){
print "$lun wwn is $1 \n";
}
}
__DATA__
LOGICAL UNIT NUMBER 587
UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11
LOGICAL UNIT NUMBER 128
UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11
LOGICAL UNIT NUMBER 763
UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular Expression help!
On 11/05/2011 16:38, jet speed wrote:
> Hi All,
>
> I need help in matching the regular expression, the file is as below.
>
> I am trying to match number followed by Number ex 587, 128 in $1 and
> 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 in $2
>
> the $1 match works find with regulare expression #if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
>
> However i am not sure how to get both $1 and $2 togather.
>
> Ideally i would like to have an output printed
>
> print "$1 wwn is $2 \n";
>
> Any help on this would be much appreciated.
>
>
> FILE
> -----------------------------------------------
>
>
> LOGICAL UNIT NUMBER 587
> UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11
>
> LOGICAL UNIT NUMBER 128
> UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11
>
> LOGICAL UNIT NUMBER 763
> UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11
>
> -----------------------------------------------------------
>
>
> #!/usr/bin/perl
> open(FILE, "clrlist") || die "Can't open clrlist : $!\n";
>
> while (<FILE>) {
> #if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
> if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})\n[a-z0-9+]\s*(\d{2}*\d{2}*\d {2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d {2}*\d{2}*\d{2}*\d{2}*)/i)
> {
> print "$1 ";
> print "$2";
> }
> }
>
> ---------------------
I think I understand what you hope to achieve, but your code will not
compile as it stands.
First of all, instead of
/\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i
you should write simply
/LOGICAL UNIT NUMBER (\d{1,4})/i
Your second regex is similar, but anything like
/\d{2}*/
gives the error 'Nested quantifiers in regex'. You can specify either
{2} (exactly two occurrences) or * (zero or more occurrences) but not both!
Also, you are trying to match a string of 16 hex bytes using /\d/. That
matches only 0..9 in ASCII (and a lot more in Unicode, but that isn't a
problem here). For hex, use either /[0-9A-X]/i or /[[:xdigit:]]/.
But the main problem is that you are trying to match two lines
simultaneously. Your regex contains /\n/, but you are reading from the
file only one line at a time. Each line will contain *either*
"LOGICAL UNIT NUMBER 587\n"
*or*
"UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11\n"
so you need to code accordingly.
On top of that, your second regex includes /[a-z0-9+]/i when I think you
meant /[a-z0-9]+/i, and it doesn't allow for the colon after 'UID'. Once
more, this should be /UID:\s*/ or something similar. Without seeing your
data I cannot help further.
For those who would help, I believe the program looks as below.
I hope this helps,
Rob
use strict;
use warnings;
while (<DATA>) {
#if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
if ($_=~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})\n[a-z0-9+]\s*(\d{2}*\d{2}*\d {2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d{2}*\d {2}*\d{2}*\d{2}*\d{2}*)/i) {
print "$1 ";
print "$2";
}
}
__DATA__
LOGICAL UNIT NUMBER 587
UID: 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11
LOGICAL UNIT NUMBER 128
UID: 60:06:01:60:50:40:21:00:D0:23:D5:C2:BA:D9:DE:11
LOGICAL UNIT NUMBER 763
UID: 60:06:01:60:50:40:21:00:25:C6:3F:A7:CA:2D:DF:11
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular Expression help!
On May 11, 8:38=A0am, speedj... [at] googlemail.com (jet speed) wrote:
> Hi All,
>
> I need help in matching the regular expression, the file is as below.
>
> I am trying to match number followed by Number ex 587, 128 in $1 and
> 60:06:01:60:42:40:21:00:3A:AA:55:37:91:8A:DF:11 in $2
>
> the $1 match works find with regulare expression =A0#if ($_=3D~
> /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
>
> However i am not sure how to get both $1 and $2 togather.
>
> Ideally i would like to have an output printed
>
> print "$1 wwn is $2 \n";
>
> Any help on this would be much appreciated.
>
> FILE
> -----------------------------------------------
>
> LOGICAL UNIT NUMBER 587
> UID: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A060:06:01:60:42:40:21:=
00:3A:AA:55:37:91:8A:DF:11
>
> LOGICAL UNIT NUMBER 128
> UID: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A060:06:01:60:50:40:21:=
00:D0:23:D5:C2:BA:D9:DE:11
>
> LOGICAL UNIT NUMBER 763
> UID: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A060:06:01:60:50:40:21:=
00:25:C6:3F:A7:CA:2D:DF:11
>
> -----------------------------------------------------------
>
> #!/usr/bin/perl
> open(FILE, "clrlist") || die "Can't open clrlist : $!\n";
>
> while (<FILE>) {
> #if ($_=3D~ /\w{7}\s\w{4}\s\w{6}\s(\d{1,4})/i) { #workts for 1st line
> ...
Also, you may want to consider the /x modifier to avoid
"regex blindness", [a visual Bermuda Triangle that will
cause anyone viewing it to avert their eyes and quickly
paddle the other way].
Even with an easy regex, the view improves considerably
with /x :
if ( / \w{7} \s \w{4} \s # insert comment here...
\d{1,4} # more comments...
....
/ix )
Or just:
/ \w{7} \s \w{4} \s .... /ix; # whitespace enhanced
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regular Expression help!
--000e0cdffe44a7399304a3070764
Content-Type: text/plain; charset=ISO-8859-1
Hi All,
Thanks for your time and valuable inputs, Appreciate it.
I will try your suggestions and test it in my program.
Sj
--000e0cdffe44a7399304a3070764--