Re: Extract range of lines from a text file

Dr.Ruud wrote:
Amer Neely schreef:

I'm walking through a mailbox file, and want to pull out specific
lines from each message. The body of each message is in a similar
format, having been generated by a script.

I'm doing OK except for one particular block of lines, the customer
address data. There is a blank line before and after this block.

Transaction Time: 18:45:55

Amer Neely
POB 1481 Station Main
North Bay ON
P1B 8K7


Or use a simplified state machine.

my $state = -1;
my $line = -1;

while (<>) {
chomp; # s/^\s+//; s/\s+$//;

if (-1 == $state) {
if (/^Transaction Time:/) {
elsif (0 == $state) {
if (/^$/) {
$line = 0;
else {
die "$state: <$_>?";
elsif (1 == $state) { # in address
if (^$) {
# skip
elsif (/^\d{3}-\d{3}-\d{4}$/) {
$state = -1;
$line = -1;
else {
print "$line: $_\n";
else {
die "$state: <$_>?";


Very interesting. It's a little more complex than I need (see the reply by Xicheng Jia dated today 11:57). That works for me.

I did adopt your code (with a few minor fixes) to my situation, but got an error when I ran it on my test input file:
Sun Apr 9 12:29:32 2006
0: <xxxxxxxxxxxx
P6A 3P4
CANADA>? at line 39, <IN> chunk 2.

Thank you for this very different approach. I will keep it in mind for other situations.
Amer Neely
Home of Spam Catcher
E: trudge@xxxxxxxxxxxxxx
Perl | MySQL | CGI programming for all data entry forms.
"We make web sites work!"

Relevant Pages

  • Re: Extract range of lines from a text file
    ... Amer Neely schreef: ... format, having been generated by a script. ... Or use a simplified state machine. ...
  • Re: how to break the bounds of 59 in date/time data type
    ... different sub-formats, whereas i want the format to be able to record ... When one designs a database, it's necessary to determine what data entities you want to track and how each entity is to be represented. ... But this is an issue with DISPLAY of your information and DATA ENTRY of your information. ...
  • Converting Excel Formula to Macro
    ... When the data entry is complete, a pre-written formula is copied to ... prep and save the the data. ... format the data into a valid XML file. ...
  • Re: Time entered (and handled as data) in simple minutes and seconds.
    ... Then do all your data entry and then use that formula to convert to ... Put 60 in an empty cell ... And format the way you want. ... single digit minutes ...
  • Re: Data entry without saving
    ... In the current version of the database, I'm restricting users from ... can a script be written so that the operator ... entry layout without saving the data which is already entered. ... Put two buttons on the layout: one for data entry and one to cancel. ...