View Single Post
  #6 (permalink)  
Old 23rd Mar 2009, 9:59 am
tayloratosu tayloratosu is offline
New Member
 
Join Date: Oct 2007
Posts: 1
Default Linux script to reformat text files for ebook reader

I've been fiddling around with the ebook reader on my mp4 player, and here's what I've discovered:

- my player displays 16 characters/line
- it ignores end-of-line characters
- it doesn't ignore extra spaces at the end of a line
- it seems to need some sort of "line reset" every 30 lines or so. Otherwise it drops a character or something.

So, I wrote a quick script (see below) in Linux that formats input text to 16 characters/line with word wrap, pads out short lines with spaces, and inserts a hard-page character (control-L) after every thirtieth line. Seems to work very nicely so far.

Sorry, this is strictly for Linux (and maybe for Cygwin under Windows). Maybe someone could write a quickie Windows program that would do the same thing? Requires awk, sed, and fold.

A couple of notes:
- Copy the text below and paste it into a text editor. I call mine format_small.sh. Save it and make it executable. (chmod u+x format_small.sh)
- This script is a pipe. Run it like this:
./format_small.sh[list=1]newfile
- the first sed line changes all tabs to single spaces. The thing between the first two slashes should be a tab character, not a space.
- the second sed line removes all leading spaces.
- the fold command reformats the lines to <= 16 char, with word wrap
- in the awk program, the SPACES variable is sixteen spaces.
- in the awk program, the CONTROL_L variable is a real control-L. In vi, you put it in by typing control-V control-L

Here's the script:
sed 's/ / /g' | \
sed 's/^ *//' | \
fold -w 16 -s | \
awk 'BEGIN {SPACES=" ";
CONTROL_L="^L";
MAXCOUNT=30;
COUNT=0;
}
{print substr($0 SPACES,1,16);
COUNT=COUNT+1;
if (COUNT == 30) {
print CONTROL_L;
COUNT=0;
}
}'
Reply With Quote