Solved

Capturing a single line within a multiline record using Perl Regex

Posted on 2011-03-15
8
446 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

785 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