Ithaca Free Software GNU Linux

A Central New York Free Software User Group
It is currently Wed Sep 08, 2010 7:38 am

All times are UTC - 4 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Shell script: Format Gutenberg.org etext for PalmOS
PostPosted: Tue Sep 12, 2006 10:10 am 
Offline
User avatar

Joined: Tue Sep 05, 2006 12:57 pm
Posts: 159
Location: Freeville, NY
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



Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 4 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group