jwkuo87 has asked for the wisdom of the Perl Monks concerning the following question:
The Perl script should extract "This is the text contained in Chapter 2 and will contain a lot of text with at least 100 words and probably somewhere around 1000-5000." from the file and write the output to a new file. Unfortunately, the code below only gives me the first matches, i.e. the text from the table of contents.Table of Contents Chapter 1. Introduction Chapter 2. Main Chapter 3. Conclusion ============================== Chapter 1. Introduction This is the introduction preceding Chapter 2. Chapter 2. Main This is the text contained in Chapter 2 and will contain a lot of text + with at least 100 words and probably somewhere around 1000-5000. Chapter 3. Conclusion This is the conclusion.
Is there a way to refine my search function so it works as I would like it to? Like including a rule where the startstring must be skipped if the preceding 5 strings contains "Chapter 1. Introduction" and/or the output should contain at least 100 words? Thanks in advance! :)#!/usr/bin/perl -w #use strict; my $startstring='Chapter\s2\.\sMain'; my $endstring='Chapter\s3\.\sConclusion'; { local $/; open (SLURP, "C:\\Text\\1.txt") or die $!; $data = <SLURP>; close SLURP or die $!; { @finds=$data=~m/($startstring.*?$endstring)/ismo; } open my $OUTFILE, ">", "C:\\Text\\Chapter2\\1.txt" or die $!; print $OUTFILE "@finds"; close $OUTFILE; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extracting a chapter from text file
by davido (Cardinal) on Apr 15, 2014 at 17:01 UTC | |
|
Re: Extracting a chapter from text file
by bigj (Monk) on Apr 15, 2014 at 14:43 UTC | |
|
Re: Extracting a chapter from text file
by duff (Parson) on Apr 15, 2014 at 14:48 UTC | |
|
Re: Extracting a chapter from text file
by kcott (Archbishop) on Apr 16, 2014 at 10:46 UTC | |
|
Re: Extracting a chapter from text file
by jwkuo87 (Initiate) on Apr 16, 2014 at 18:59 UTC |