#! /usr/local/bin/perl # split stanzas with multiple entries into more stanzas with unique entries # read input file if (read (STDIN, $text, 400000) == 400000) { die "input file too long (>= 400000 bytes)"; }; close (STDIN); @text = split (/\s*\n\s*\n\s*/, $text); undef $text; foreach $stanza (@text) { @lines = split (/\s*\n\s*/, $stanza); for ($i=0; $i<=$#lines; $i++) { if ($lines[$i] =~ /([^:]*):/) { $hdr = $1; $bdy = $'; $hdr = &strip ($hdr); $bdy = &strip ($bdy); $lines[$i] = "$hdr:$bdy"; } elsif (! /^\s*(\#.*)?$/) { die "illegal line: $lines[$i]"; }; }; @lines = sort @lines; undef @ix; undef @iz; for ($i=0; $i<=$#lines; $i++) { $lines[$i] =~ /([^:]*):/; $hdr[$i] = $1; $bdy[$i] = $'; if (! $i || ($hdr[$i] ne $hdr[$i-1])) { push (@iz, $i); }; }; push (@iz, $#lines+1); undef @lines; @ix = @iz; pop (@ix); LOOP: for (;;) { foreach $i (@ix) { print "$hdr[$i]: $bdy[$i]\n"; }; print "\n"; for ($i=$#ix; $i>=0; $i--) { $ix[$i]++; if ($ix[$i] >= $iz[$i+1]) { last LOOP if $i==0; $ix[$i] = $iz[$i]; } else { last; }; }; }; }; sub strip { local ($_) = $_[0]; local ($x, $y); if (/^\s*$/) { return ''; } else { /^\s*(\S)/; $x = $1; $y = $'; if ($y =~ /(\S)\s*$/) { return $x . $` . $1; } else { return $x; }; }; }