#!/usr/bin/perl use strict; use warnings; $/ = ""; # empty string sets "paragraph mode": reads up-to/including blanks my @parts; my $xml_format = "\n%s\n". " \n%s\n \n". " \n%s\n \n". " \n%s\n \n\n"; print "\n"; while () { s/\n+$//; # trim off trailing line-breaks if ( /^\s*\d/ ) { # record begins with a number: start of new recipe 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 procedure } else { # this is either the abstract, ingredients or start of procedure push @parts, $_; } } printf( $xml_format, @parts ) if ( @parts ); print "\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.