need a newline between paragraphs....

Gary Kline kline at
Tue Nov 24 00:29:31 UTC 2009

	some several weeks ago, i got the appended perl code sent that
	takes a very long txt file and diving by ``Chapter NN'' puts
	the long file into seperate files, 01 to 66.  trouble is that
	it is hard to read by eyes without a space between paragraphs.

	it's easy for grep to remove all newlines.  How do i add them



	PS: having long, wrapped lines would work best


#!/usr/bin/perl -w
#<txt2ch: split text into chapters

use strict;
my ($fh, $preamble, $regex);

# Uncomment ONE of these depending on filetype.
# $/ = $\ = "\r\n";   # DOS
$/ = $\ = "\n";       # Unix

# Specify the chapter divider once: $1 holds the chapter number.
$regex = qr/^\s*Chapter\s+(\d\d*)/;

# Get anything before the first chapter.
$preamble = '';

while (<>) {
    if (/$regex/) {
        $fh = newchapter($1);
        print $fh "$preamble" . "$_";
    $preamble .= "$_" . "$/";

# We've read at least one chapter heading.
while (<>) {
    $fh = newchapter($1) if /$regex/;
    next if /^#/;
    print $fh "$_";


# Open a new chapter if we match the regular expression.
sub newchapter {
    my $n = shift || die "need a number";
    my $file = sprintf("%2.2d", $n);
    close($fh) if defined($fh);
    open($fh, "> $file") or die "$file: $!\n";
    return $fh;


FWIW: here are a few paragraphs from chapter one


Chapter One

"We're here," Erik said to his date as he swung his van into the diabled slot of
the Blue Note Tavern's parking lot.  For the slightest moment he was
awkstruck at how pretty she was.  That the girl was blind meant
nothing to Erik.  She could've been blind, deaf, and missing her right
leg.  The only thing he was certain of was that he wanted her--maybe
worse that he'd wanted anything. *Ever.*

The radio interrupted with the weather forecast just then. "It's cold out there,
folks, so bundle up!  It's 21 degrees here in Pine Falls, but the wind
chill is a brisk minus 23 to around 40 degrees.  Now the forecast for all
of Northwest Wisconsin:  From 10 to 20 degrees with dangerously cold
wind chills ranging to minus 40 degrees.  Less windy and much warmer
tomorrow and even warmer on Sunday."

"Typical November weather," he said, but no problem," he said.  "We're right 
by the front door!"

"You're in a wheelie spot?" she asked.

"You bet!"

Dawn said, "My brother Morgan says it's wrong to have this.
He thinks it's reverse discrimination; that whoever gets to
the nearest parking space first ought to get it.  I tell him,
God forbid he should ever be disabled!"

More information about the freebsd-questions mailing list