Re-formatting badly formatted text

You’ll find lots of badly formatted text in (pdf) ebooks and such. I used this script to quickly reformat most of the text:

#!/usr/bin/perl

while(<>) {

# remove stray newlines
s/(\w|\,|\;|\:)\s*\n/$1 /gm;

# remove hypenation
s/(\w)\-\s*\n/$1/gm;

# print the row
print;

}

Invoke the script like this:

$ ebookfmt.pl < unformatted.txt > formatted.txt

Then you can import it in Libreoffice and export again as a PDF or save the odt and create an epub or mobi with Calibre.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s