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:


while(<>) {

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

# remove hypenation

# print the row


Invoke the script like this:

$ < 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.