Code:
#!/bin/bash
# replace_line_breaks.sh
# --------------------------------------------------------------------
# Copyright 2005 Mitch Wiedemann (mc2@lightlink.com)
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/gpl.txt
# --------------------------------------------------------------------
# this shell script reformats text files so that the paragraphs have no internal line breaks
# I use it primarily to reformat Gutenberg etexts for use on my PalmOS device
# this script is probably wildly inefficient, but it works. Feel free to suggest improvements.
# the tofrodos package is required to do the dos -> unix line break conversion.
if [ -f "$1" ]
then
# make a backup of the input file
cp $1 $1.bak
# make a working copy of the input file
cp $1 z9zz1
# change DOS/Windows style line breaks (CR/LF) to Unix style (LF)
dos2unix z9zz1
# replace all occurrences of two line breaks with a dummy string (###)
perl -0777 -pe 's/\n\n/\#\#\#/g' z9zz1 > z9zz2
# replace all single line breaks with a space
perl -0777 -pe 's/\n/ /g' z9zz2 > z9zz3
# replace all the dummy strings (###) with two line breaks
perl -0777 -pe 's/\#\#\#/\n\n/g' z9zz3 > z9zz4
# remove all leading spaces from the lines of text
perl -0777 -pe 's/^ +//gm' z9zz4 > z9zz5
# copy the working copy back to the original input file
cp z9zz5 $1
# remove all of the working copyies created in the steps above.
rm z9zz*
else
echo "You must specify a valid filename.";
fi