Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Nov 2009 16:29:23 -0800
From:      Gary Kline <kline@thought.org>
To:        FreeBSD Mailing List <freebsd-questions@FreeBSD.ORG>
Subject:   need a newline between paragraphs....
Message-ID:  <20091124002920.GA51110@thought.org>

next in thread | raw e-mail | index | archive | help

	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
	back?

	tia 

	gary

	PS: having long, wrapped lines would work best

	ENCL: txt2chapts.pl







#!/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 (<>) {
    chomp;
    if (/$regex/) {
        $fh = newchapter($1);
        print $fh "$preamble" . "$_";
	last;
    }
    $preamble .= "$_" . "$/";
}

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

close($fh);
exit(0);

# 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!"





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20091124002920.GA51110>