Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex for Multi line

Status
Not open for further replies.

skar

MIS
Mar 21, 2001
37
GB
Hi all,

I'm trying to grab data from a txt file. I can find the lines I want but having problems because the txt file wraps the text and I cant unwrap it!

Here's some sample code I've been playing with.

=======
#!c:/perl/bin/perl -w

use strict;

open(SURVEY, "c:\\compaq\\survey.txt") || die "Sorry, there was a problem opening the file.\n";
while (<SURVEY>) {
if(m/^Firmware Revisions(.*?)(\d\d\/\d\d\/\d\d)$/sgm) {
print $_;
}
}
close(SURVEY);

=======

The regex should find a line starting with "Firmware Revisions" and then ignore everything until it see's a date in brackets "(10/10/04)"
The date is on the next line. And thats my problem. I've googled and read up on the subject. Think I'm getting near but the deadline for this script is just about to whoooosh on by... :)

So any help would be fab!
 
use a switch;


$doprint = 0;
while (<SURVEY>) {
$doprint = 1 - $doprint if(
...Firmware Revisions... ||
...(\d\d\/\d\d\/\d\d)... ));

print(...) if($doprint);
}

format the code, for readability, please don't use colors
 
sorry, does not work

just:

print(...) if( ...Firmware Revisions... ||
...(\d\d\/\d\d\/\d\d)... ));
 
but you are on unix, use sed

sed -ne '/startregexp/,/stopregexp/p' input >output
 
This should give you a place to start.

Code:
while (<DATA>) {
    if (/Firmware Revisions/) {
        $_ = <DATA>;    #Read Next Line
        m{(\(\d{2}/\d{2}/\d{2}\))};
        print "Firmware Revisions: $1\n";
    }
}

__DATA__
Blah Blah... here's some text
Firmware Revisions - here's some more text
and here's the date (10/10/04)
 
Excellent stuff guys. I've now got a working model and can create the other "recipes" that I need.

Many thanks and I'll keep away from the [color] tags next time. :)

Cheers!
 
Or, If the survey.txt file is not large (relative to the availible RAM) and if you might be doing multiple regex's against the file contents, you might pull the file into a var and surf the entire file for the patterns of interest.


Code:
open(SURVEY, "c:\\compaq\\survey.txt") || die "Sorry, there was a problem opening the file.\n";
while (<SURVEY>) { $file_contents .= $_;  }
close(SURVEY);

while ($file_contents =~ /Firmware Revisions(.*?)(\d\d\/\d\d\/\d\d)$/sgm/)
	{
	print "$&\n";
	}

TMTOWTDI - as usual ;-)

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top