As long as your input files are not too large it might be easier to slurp the entire file into memory as a string and capture each document in turn using a global regex match. Here, I capture the document in $1 and, within it, the case name in $2. I have put the data (including a second document) after the __END__ token in the script so I can read it with the <DATA> filehande which Perl automatically opens for us.

use strict; use warnings; my $allDocs = do { local $/; <DATA>; }; my $rxExtractDoc = qr {(?xms) ( \d+\sof\s\d+\sDOCUMENTS .*? No\.\s(\w+) \n ) }; while ( $allDocs =~ m{$rxExtractDoc}g ) { my $document = $1; my $caseName = $2; my $edCount = () = $document =~ m{EDUCATION}g; print qq{Case Name: $caseName\n}, qq{Education: $edCount\n}, q {-} x 25, qq{\n}; } __END__ Blurfl Blurfl No. 32323IY883 97 of 141 DOCUMENTS HOKE COUNTY BOARD OF EDUCATION; HALIFAX COUNTY BOARD OF EDUCATION; ROBESON COUNTY BOARD OF EDUCATION; CUMBERLAND COUNTY BOARD OF EDUCA\TION; VANCE COUNTY BOARD OF EDUCATION; RANDY L. HASTY, individually and as guardian ad litem of Randell B. Hasty; STEVEN R. SUNKEL, individually and as guardian ad litem of Andrew J. Sunkel; LIONEL WHIDBEE, individually and as guardian ad litem of Jeremy L. Whidbee; TYRONE T. WILLIAMS, individually and as guardian ad litem of Trevelyn L. Williams; D.E. LOCKLEAR, JR., individually and as guardian ad litem of Jason E. Locklear; ANGUS B. THOMPSON II, individually and as guardian ad litem of Vandaliah J. Thompson; MARY ELIZABETH LOWERY, individually and as guardian ad litem of Lannie Rae Lowery; JENNIE G. PEARSON, individually and as guardian ad litem of Sharese D. Pearson; BENITA B. TIPTON, individually and as guardian ad litem of Whitney B. Tipton; DANA HOLTON JENKINS, individually and as guardian ad litem of Rachel M. Jenkins; LEON R. ROBINSON, individually and as guardian ad litem of Justin A. Robinson, Plaintiffs and ASHEVILLE CITY BOARD OF EDUCATION; BUNCOMBE COUNTY BOARD OF EDUCATION; CHARLOTTE-MECKLENBURG BOARD OF EDUCATION; DURHAM PUBLIC SCHOOLS BOARD OF EDUCATION; WAKE COUNTY BOARD OF EDUCATION; WINSTON-SALEM/FORSYTH COUNTY BOARD OF EDUCATION; CASSANDRA INGRAM, individually and as guardian ad litem of Darris Ingram; CAROL PENLAND, individually and as guardian ad litem of Jeremy Penland; DARLENE HARRIS, individually and as guardian ad litem of Shamek Harris; NETTIE THOMPSON, individually and as guardian ad litem of Annette Renee Thompson; OPHELIA AIKEN, individually and as guardian ad litem of Brandon Bell, Plaintiff-Intervenors v. STATE OF NORTH CAROLINA and the STATE BOARD OF EDUCATION, Defendants No. 530PA02 98 of 141 DOCUMENTS HOKE COUNTY BOARD OF EDUCATION; HALIFAX COUNTY BOARD OF EDUCATION; CUMBERLAND COUNTY BOARD OF EDUCATION; VANCE COUNTY BOARD OF EDUCATION; RANDY L. HASTY, individually and as guardian ad litem of Randell B. Hasty; STEVEN R. SUNKEL, individually and as guardian ad litem of Andrew J. Sunkel; LIONEL WHIDBEE, individually and as guardian ad litem of Jeremy L. Whidbee; TYRONE T. WILLIAMS, individually and as guardian ad litem of Trevelyn L. Williams; D.E. LOCKLEAR, JR., individually and as guardian ad litem of Jason E. Locklear; ANGUS B. THOMPSON II, individually and as guardian ad litem of Vandaliah J. Thompson; MARY ELIZABETH LOWERY, individually and as guardian ad litem of Lannie Rae Lowery; JENNIE G. PEARSON, individually and as guardian ad litem of Sharese D. Pearson; BENITA B. TIPTON, individually and as guardian ad litem of Whitney B. Tipton; DANA HOLTON JENKINS, individually and as guardian ad litem of Rachel M. Jenkins; LEON R. ROBINSON, individually and as guardian ad litem of Justin A. Robinson, Plaintiffs and ASHEVILLE CITY BOARD OF EDUCATION; BUNCOMBE COUNTY BOARD OF EDUCATION; WINSTON-SALEM/FORSYTH COUNTY BOARD OF EDUCATION; CASSANDRA INGRAM, individually and as guardian ad litem of Darris Ingram; CAROL PENLAND, individually and as guardian ad litem of Jeremy Penland; DARLENE HARRIS, individually and as guardian ad litem of Shamek Harris; NETTIE THOMPSON, individually and as guardian ad litem of Annette Renee Thompson; OPHELIA AIKEN, individually and as guardian ad litem of Brandon Bell, Plaintiff-Intervenors v. STATE OF NORTH CAROLINA and the STATE BOARD OF EDUCATION, Defendants No. 534PA08 End of document set

Here's the output.

Case Name: 530PA02 Education: 11 ------------------------- Case Name: 534PA08 Education: 8 -------------------------

I hope this is of use.

Cheers,

JohnGG


In reply to Re: Capture/Counting text between patterns across multiple lines by johngg
in thread Capture/Counting text between patterns across multiple lines by micwood

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.