Solved

Capturing a single line within a multiline record using Perl Regex

Posted on 2011-03-15
8
465 Views
Last Modified: 2012-05-11
Here's the data record content below:

__Data__
@test_scsi_1
memory test
windows (xp)
1.  ddkddlakldjkla;djkfla;jdlafklda;fkla;dkdkslsls
2.  dkskdslfjklsjfkdsjkldjskldjflksjdfklsjfkdlsjkflds
3.  dhskhdksfdsfjdskljflksjfkdlsjkfldjksldjklfsjkldsfj

__Data__
@test_scsi_2
scatter-gatter test
windows (vista)
1.  fskdjfklsjdfksjkfdjskldjfklsjfkdlsjklfjsdkljflkds
2.  djfkldsjfklsjdklsjfkldjsklfjdklsjfkldsjfklsjdkljfsl
windows (vists)
fjksjfdklsjfkldsjklfjdsljfklsklfs
djflsdjkflsjdkflsjfkldsjklfjsklfds

__Data__
@test_scsi_3
block_size test (windows 7)
fjsljfklsdklfsjkdljflsjdklfsjkdljfklsjdklfjsklfdj
dsfdslfjklsjflkdsjflsjdflsjkfjslkfjklsjflksjfklsjfk
windows (7)
djfsjdklfjskdljflsjdklfs
fdskfjdlsjfklsjdfklsjdklfjsljdkflsjdklfjslkjflks


The code below that I wrote was an attempt to capture a this single line (@test_scsi_x) from the multiline records mention above. But the problem is that it capture all lines.

#!/usr/bin/perl

use warnings;
use strict;

$/='__Data__';

   open 'FH','<',"/home/scsi_data_log/";
    while(<FH>)
   {

       if(/^(\@.*[^\n]*)/ms)
       {
          print $1;
       }

    }
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
  • 4
  • 2
  • 2
8 Comments
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35139767
try this





#!/usr/bin/perl

use warnings;
use strict;


   open 'FH','<',"./sing.txt";
    while(<FH>)
   {

       if(/^\@.*$/)
       {
          print $_;
       }

    }

0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35139787
ooops wrong file path



#!/usr/bin/perl

use warnings;
use strict;


   open 'FH','<',"/home/scsi_data_log/";
    while(<FH>)
   {

       if(/^\@.*$/)
       {
          print $_;
       }

    }

0
 

Author Comment

by:areyouready344
ID: 35139911
Thanks but is there a solution to use with capture feature of regular expression because I have other lines within this multi-line i would like to capture as well.
0
Industry Leaders: 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: 35142267
How about this?

#!/usr/bin/perl

use warnings;
use strict;

$/='__Data__';

   open 'FH','<',"perl_regex_match4.txt";
    while(<FH>)
   {

       if(/^(\@[^\n]*\n)/ms)
       {
          print $1;
       }

    }

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35142286
(You'll need to correct the file path first)
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 35142293
Note that if you don't want to capture the \n after the @test_scsi_# value, then use this pattern:
       if(/^(\@[^\n]*)/ms)
0
 

Author Comment

by:areyouready344
ID: 35143833
Thanks a million TerryAtOpus, its working as requested. It made a big different by removing .* next to the @ symbol. I have another weird problem but will put it in another ticket..
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35143843
Just to clarify, when you are using the s pattern modifier, the . wildcard will also match newlines, so .* will match everything.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
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…
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…

749 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