Bash script which adds space inside long words in Pages file

106 views Asked by At

I like to convert documents to EPUB format because it is easier for me to read. However, if I do this for for example some code documentation, some really long lines of code are not readable in the EPUB, because they trail off-screen. I would like to automatically insert spaces in any words in a text file (specifically, a Pages document) over a certain length, so they are reduced to say, 10 character words, at maximum. Then, I will convert that Pages document to an EPUB.

How can I write a bash script which goes through a Pages document and inserts spaces into any word longer than, perhaps, 10 characters?

1

There are 1 answers

1
Shawn On

sed is your friend:

$ cat input.txt
a file with a
verylongwordinit to test with.
$ sed 's/[^[:space:]]\{10\}/& /g' input.txt
a file with a
verylongwo rdinit to test with.

For every sequence of 10 non-whitespace characters in each line, add a space after (The & in the replacement text is itself replaced with the matched text).

If you want to change the file inline instead of making a copy, ed comes into play:

ed input.txt <<'EOF'
s/[^[:space:]]\{10\}/& /g
w
EOF

(Or some versions of sed take an -i switch for inline editing)