Solved

Capturing a single line within a multiline record using Perl Regex

Posted on 2011-03-15
8
424 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 8

Expert Comment

by:point_pleasant
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 35

Expert Comment

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

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Just to clarify, when you are using the s pattern modifier, the . wildcard will also match newlines, so .* will match everything.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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