Solved

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

Posted on 2011-03-22
7
394 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl count the hash for print 4 161
Passing shell scripts to run on the SFTP cmd object 12 180
Port 80 requests 16 87
Migrate OTRS to newest version. 1 312
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

24 Experts available now in Live!

Get 1:1 Help Now