Solved

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

Posted on 2011-03-22
7
428 Views
Last Modified: 2012-08-13
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;
0
Comment
Question by:areyouready344
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 35194117
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
 

Author Comment

by:areyouready344
ID: 35194576
yes Terry, this is what I'm looking, thanks a million. I need to test it
0
 

Author Comment

by:areyouready344
ID: 35194591
I need it to print an individual line from a multiline data record.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Terry Woods
ID: 35194595
Can you please explain & provide an example?
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35194926
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35195080
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
 

Author Comment

by:areyouready344
ID: 35199712
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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 70
How to delete rows in a file based on the file name. 5 73
Rename file based on contents in the file? 3 107
PHP equivalent of opening a com object 5 101
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question