Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

data extract - Help !

18 views
Skip to first unread message

jet speed

unread,
Nov 13, 2012, 6:44:26 AM11/13/12
to Perl Beginners
Hi All,

I have a file with similar data, around 10,000 entries with similar format.

i need to extract the output in below format. I can match the WWN and push
into an arrray, however i am not sure how to reference the WWN to its
corresponding device displayDevNum as in the below format.
Any help would be much appreciated.

I am using test data here due to system restrictions.


required output
------------------------
10:79 10.00.00.00.00.C0.43.33.AB
10:79 10.00.00.00.00.C0.43.33.A2
10:99 10.00.00.00.00.C0.22.33.56
10:99 10.00.00.00.00.C0.34.33.A0
10:99 10.00.00.00.00.C0.22:49:33
10.55 10.00.00.00.00.C9.43.42.B6
10.55 10.00.00.00.00.C0.43.23.C9

DATA - file.txt
--------------------
devNum=4,177
displayDevNum=10:79
LUN=121
WWN=10.00.00.00.00.C0.43.33.AB
nickname=a5
WWN=10.00.00.00.00.C0.43.33.A2
nickname=wacke22
devNum=4,177
displayDevNum=10:99
LUN=121
WWN=10.00.00.00.00.C0.22.33.56
nickname=a2
WWN=10.00.00.00.00.C0.34.33.A0
nickname=ajx
WWN=10.00.00.00.00.C0.22:49:33
nickname=yah1
devNum=4,177
displayDevNum=10:55
LUN=121
WWN=10.00.00.00.00.C9.43.42.B6
nickname=a52
WWN=10.00.00.00.00.C0.43.23.C9
nickname=wack1


Thanks
Sj

Shlomi Fish

unread,
Nov 13, 2012, 7:12:35 AM11/13/12
to Perl Beginners
Hi Jet Speed,

On Tue, 13 Nov 2012 11:44:26 +0000
jet speed <spee...@googlemail.com> wrote:

> Hi All,
>
> I have a file with similar data, around 10,000 entries with similar format.
>
> i need to extract the output in below format. I can match the WWN and push
> into an arrray, however i am not sure how to reference the WWN to its
> corresponding device displayDevNum as in the below format.
> Any help would be much appreciated.
>

You can keep the displayDevNum in a variable global to the loop. Like this:

#!/usr/bin/perl

use strict;
use warnings;

my $last_dev_num;

while (my $line = <>)
{
chomp($line);
if (my ($dev_num) = $line =~ /\AdisplayDevNum=(.*)\z/)
{
$last_dev_num = $dev_num;
}
elsif (my ($wwn) = $line =~ /\AWWN=(.*)\z/)
{
print "$last_dev_num $wwn\n";
}
}

Regards,

Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

Chuck Norris is not afraid of superstitions. Superstitions are afraid of Chuck
Norris. (via Dov Levenglick)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

jet speed

unread,
Nov 13, 2012, 12:02:51 PM11/13/12
to Shlomi Fish, Perl Beginners
Hi Shlomi, Appreciate your help, your loop works perfectly !.
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>

John W. Krahn

unread,
Nov 14, 2012, 12:01:27 AM11/14/12
to Perl Beginners
$ echo 'devNum=4,177
displayDevNum=10:79
LUN=121
WWN=10.00.00.00.00.C0.43.33.AB
nickname=a5
WWN=10.00.00.00.00.C0.43.33.A2
nickname=wacke22
devNum=4,177
displayDevNum=10:99
LUN=121
WWN=10.00.00.00.00.C0.22.33.56
nickname=a2
WWN=10.00.00.00.00.C0.34.33.A0
nickname=ajx
WWN=10.00.00.00.00.C0.22:49:33
nickname=yah1
devNum=4,177
displayDevNum=10:55
LUN=121
WWN=10.00.00.00.00.C9.43.42.B6
nickname=a52
WWN=10.00.00.00.00.C0.43.23.C9
nickname=wack1' | perl -e'

my $displayDevNum;
while ( <> ) {
$displayDevNum = $1 if /^displayDevNum=(\d\d:\d\d)/;
print "$displayDevNum $1\n" if /^WWN=([0-9a-f.:;]+)/i;
}
'
10:79 10.00.00.00.00.C0.43.33.AB
10:79 10.00.00.00.00.C0.43.33.A2
10:99 10.00.00.00.00.C0.22.33.56
10:99 10.00.00.00.00.C0.34.33.A0
10:99 10.00.00.00.00.C0.22:49:33
10:55 10.00.00.00.00.C9.43.42.B6
10:55 10.00.00.00.00.C0.43.23.C9





John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

Dr.Ruud

unread,
Nov 14, 2012, 4:33:28 AM11/14/12
to begi...@perl.org
On 2012-11-13 13:12, Shlomi Fish wrote:

> while (my $line = <>)
> {
> chomp($line);
> if (my ($dev_num) = $line =~ /\AdisplayDevNum=(.*)\z/)

In Perl5, '\z' is not needed here, because '.' matches non-newlines.
(also the chomp is not needed here, looks like cargo cult to me)

--
Ruud


Shlomi Fish

unread,
Nov 14, 2012, 4:54:08 AM11/14/12
to begi...@perl.org
Hi Dr. Ruud,
You are right that the \z is not needed. It does not hurt though. However, if
you omit the chomp and include the \z, then the code will fail. I know what
chomp does, and it is a good idea to always include it, unless you are
interested in keeping the \n (cargo cult or not).

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
My Public Domain Photos - http://www.flickr.com/photos/shlomif/

Do one thing every day that scares you.
— Eleanor Roosevelt
0 new messages