How select and group certain lines in a multiline record using regular expression in Perl?

Using the following data record below, how can select the lines with the arrow for grouping using Perl's regular expression?

Here's the I think it should be but its not working.
$/='__Data__';

/^\@[^\n]*)(.*)(windows.*[^\n]*)(.*)(window.*[^\n]*)(.*)/ms;

__Data__
@test_scsi_1 <----------- Need this line group ---------------------------------------
memory test
windows (xp) <----------- Need this line group ---------------------------------------
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj
windows (vista) <---------Need this line group --------------------------------------
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds


Here's how I think it should be but its not working.
$/='__Data__';

s/(^\@[^\n]*)(.*)(windows.*[^\n]*)(.*)(window.*[^\n]*)(.*)/$1,$2,$3,$4,"__Data__",$5,$6;
areyouready344Asked:
Who is Participating?
 
Terry WoodsIT GuruCommented:
Did you want something like this?:

#!/usr/local/bin/perl -w

my $data = "\@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds"; 

$data =~ /(^\@[^\n]*)(.*?)(windows[^\n]*)(.*)(windows[^\n]*)(.*)/ms;

print "Group 1: $1\n";
print "Group 2: $2\n";
print "Group 3: $3\n";
print "Group 4: $4\n";
print "Group 5: $5\n";
print "Group 6: $6\n";

--------------
Results:
--------------
Group 1: @test_scsi_1
Group 2:
memory test

Group 3: windows (xp)
Group 4:
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl

Group 5: windows (vists)
Group 6:
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds

Open in new window

0
 
areyouready344Author Commented:
yes Terry, this is what I'm looking, thanks a million. I need to test it
0
 
areyouready344Author Commented:
I need it to print an individual line from a multiline data record.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Terry WoodsIT GuruCommented:
Can you please explain & provide an example?
0
 
farzanjCommented:
Hi,

The following program selects the files you mentioned above.
#!/usr/bin/perl

my $file = 'data';
open(FILE,$file);
@lines = <FILE>;
close(FILE);

@result = grep (/xp|vista/, @lines);

print @result;

Open in new window

0
 
Terry WoodsIT GuruCommented:
You seem to be wanting something entirely different now? I'm a bit confused as to what the end result should be.

Maybe something like this is what you want?
#!/usr/local/bin/perl

my $file = 'file.txt';
open(FILE, $file);
while (<FILE>) {
  if ($_ =~ /xp|vista/) {
    print $_;
  }
}
close(FILE);

---------
Output:
---------
windows (xp)
windows (vista)

Open in new window

0
 
areyouready344Author Commented:
Sorry for the confusion Terry, you got it right the first time. I need to be able to search within a multiline data record by using memory grouping. Once I understand how it works (thanks to your help) then I can use search and replace. The mistake I was making, I forgot use (.*?), I was using (.*), which grabbed more lines then I wanted.


This is working...

    while (<FH>) {

    if(/(^\@[^\n]*)(.*?)(windows[^\n]*)(.*?)(windows[^\n]*)(.*)/ms)
    {

         print "__Data__\n",$1,$2,$3,$4,"__Data__\n",$1,"\n",$5,$6;

    }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.