in reply to a simple match and replace script

Hello Datz_cozee75,

If your document isn’t too long, this approach should do what you want:

use strict; use warnings; use Data::Dump; my $first = 1; my $clause_number = 1; my @paragraphs; while (my $line = <DATA>) { if ($line =~ / ^ \d+ \. /x) { $line =~ s/ ^ \d+ /$clause_number/x; push @paragraphs, [ $line ]; ++$clause_number; $first = 0; } elsif ($first) { push @paragraphs, [ $line ]; $first = 0; } else { push @{ $paragraphs[-1] }, $line; } } for my $p (@paragraphs) { pop @$p while $p->[-1] eq "\n"; } dd \@paragraphs; __DATA__ Title 21. ASSIGNMENT: RESIDENT agrees not to transfer, assign or sublet the +premises or any part thereof. 22. PARTIAL INVALIDITY: Nothing contained in this Agreement shall be c +onstrued as waiving any of the OWNER'S or RESIDENT'S rights under the + law. If any part of this Agreement shall be in conflict with the law +, that part shall be void to the extent that it is in conflict, but s +hall not invalidate this Agreement nor shall it affect the validity o +r enforceability of any other provision of this Agreement. 32. RECEIPT OF AGREEMENT: The undersigned RESIDENTS have read and unde +rstand this Agreement and hereby acknowledge receipt of a copy of thi +s Barter Agreement. RESIDENT'S Signature _________________________________________________ +__ Date__________________ OWNER'S or Agent's Signature _________________________________________ +___ Date__________________

Output:

13:45 >perl 1796_SoPW.pl [ ["Title\n"], [ "1. ASSIGNMENT: RESIDENT agrees not to transfer, assign or sublet +the premises or any part thereof.\n", ], [ "2. PARTIAL INVALIDITY: Nothing contained in this Agreement shall +be construed as waiving any of the OWNER'S or RESIDENT'S rights under + the law. If any part of this Agreement shall be in conflict with the + law, that part shall be void to the extent that it is in conflict, b +ut shall not invalidate this Agreement nor shall it affect the validi +ty or enforceability of any other provision of this Agreement.\n", ], [ "3. RECEIPT OF AGREEMENT: The undersigned RESIDENTS have read and +understand this Agreement and hereby acknowledge receipt of a copy of + this Barter Agreement.\n", "\n", "RESIDENT'S Signature ____________________________________________ +_______\n", "\n", "Date__________________\n", "\n", "OWNER'S or Agent's Signature ____________________________________ +________\n", "\n", "Date__________________\n", ], ] 13:45 >

Then you can output @paragraphs with as many blank lines between each paragraph as you choose.

Update: Changed $paragraph to $clause_number in line with talexb’s good advice below.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: a simple match and replace script
by talexb (Chancellor) on Aug 10, 2017 at 13:32 UTC

    Quick comment: I would replace the scalar $paragraph with something a little less confusing like $clause_number, since you already have an array called @paragraphs.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.