Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-22
7
Medium Priority
?
448 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 …
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…
Six Sigma Control Plans

715 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