If you are confident that all the blank lines are really blank (no spaces or tabs), and if the organization of each recipe really has the same sequence of elements every time, you can read in "paragraph mode" (see the description of the INPUT_RECORD_SEPARATOR variable $/ in perlvar):
#!/usr/bin/perl use strict; use warnings; $/ = ""; # empty string sets "paragraph mode": reads up-to/including +blanks my @parts; my $xml_format = "<recipe>\n<title>%s</title>\n". " <abstract>\n%s\n </abstract>\n". " <ingredients>\n%s\n </ingredients>\n". " <procedure>\n%s\n </procedure>\n</recipe>\n"; print "<cookbook>\n"; while (<DATA>) { s/\n+$//; # trim off trailing line-breaks if ( /^\s*\d/ ) { # record begins with a number: start of new rec +ipe if ( @parts ) { # print previous recipe if there was one printf( $xml_format, @parts ); @parts = (); } push @parts, $_; } elsif ( @parts == 4 ) { # we have title, abstract, ingredients and + some procedure $parts[3] .= "\n\n$_"; # so just append this paragraph to pro +cedure } else { # this is either the abstract, ingredients or start of proc +edure push @parts, $_; } } printf( $xml_format, @parts ) if ( @parts ); print "</cookbook>\n"; __DATA__ 1.TITLE OF FIRST RECIPE abstract Ingredient 1. Ingredient 2. Ingredient n... Procedure... 2.TITLE OF SECOND RECIPE second abstract rum cola ice Just mix it all in a glass, drink it and be happy.
(updated opening sentence to make better sense; also removed an unnecessary array from the script)

In reply to Re: Regexp and reading a file n-lines at time by graff
in thread Regexp and reading a file n-lines at time by epimenidecretese

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.