Solved

Capturing a single line within a multiline record using Perl Regex

Posted on 2011-03-15
8
437 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
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
 
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
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: 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

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

12 Experts available now in Live!

Get 1:1 Help Now