Help Parsing A Text File

G

greggiefen

Hello,

I am new to Perl but have programmed in other languages in school. I
am trying to parse a VERY large text file. I initially tried to load
it all into an array but that seemed to fail due to memory constraints.

This is what i'm trying to do:#!/usr/bin/perl -w

open(OF, "output.txt");

while ($line = <OF>) {

#If a line contains this i wanted it printed so the date will be
included
if($line =~ /Page 1/) {
print $line;
}

#I want all the records regarding Mr. Smith printed. However the
records span multiple lines
if($line =~ /Mr Smith/) {
print $line;

#the lines of the peronsal records end in a line of all asterix
while ($line =~ /[^\*]/) {
print $line;
#do something magical here
}
}

}

What I need to do is to somehow get the perl script to jump to the next
line after printing the current line in the last loop, but still stay
in the loop until it reached the line of all **. If i could get all of
it into an array i could simply incriment the index until i reched the
line of all asterixs.
 
A

anno4000

greggiefen said:
Hello,

I am new to Perl but have programmed in other languages in school. I
am trying to parse a VERY large text file. I initially tried to load
it all into an array but that seemed to fail due to memory constraints.

This is what i'm trying to do:#!/usr/bin/perl -w

open(OF, "output.txt");

while ($line = <OF>) {

#If a line contains this i wanted it printed so the date will be
included
if($line =~ /Page 1/) {
print $line;
}

Is there a question involved?
#I want all the records regarding Mr. Smith printed. However the
records span multiple lines
if($line =~ /Mr Smith/) {
print $line;

#the lines of the peronsal records end in a line of all asterix
while ($line =~ /[^\*]/) {
print $line;
#do something magical here
}
}

}

What I need to do is to somehow get the perl script to jump to the next
line after printing the current line in the last loop, but still stay
in the loop until it reached the line of all **. If i could get all of
it into an array i could simply incriment the index until i reched the
line of all asterixs.

That's what the ".." operator does in scalar context. Try

print if /Mr Smith/ .. /^\*+$/;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top