Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capturing a single line within a multiline record using Perl Regex

Posted on 2011-03-15
8
Medium Priority
?
488 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

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!

Question has a verified solution.

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

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 …
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…
Six Sigma Control Plans

604 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