Solved

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

Posted on 2011-03-22
7
383 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Tutorial 6 127
Awk Question 2 116
How to generate square thumbnail using perl 13 68
Perl string filter 5 63
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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now