Join 3,563 readers in helping fund MetaFilter (Hide)


How can I add line breaks to a text file?
January 26, 2011 2:20 AM   Subscribe

How can I add line breaks to a text file? I'd like to insert line breaks into a text file so that each line of the text is no more than 33 characters, and I want to avoid line breaks inserted in the middle of words. I've seen online tools that add line breaks every X characters exactly, but this results in words cut in half. Is there an easy way to do what I'm hoping for? If I could just set the line length on Gmail, say, to 33 characters and email myself the text, that would be what I'm looking for. Thanks!
posted by surenoproblem to Computers & Internet (4 answers total) 1 user marked this as a favorite
 
Easy depending on your system... The standard Unix text utility for this is fmt:

fmt -w 33 file.txt > output.txt

The simple Perl one-liner is about the same:

perl -MText::Wrap -e '$Text::Wrap::columns=33;print wrap("","",<>)' foo.txt > output.txt

These commands usually provide ways to set the leading indentation and whether to re-flow the text or just split long lines.


$ fmt -w 33 foo.txt
How can I add line breaks to a
text file? I'd like to insert
line breaks into a text file
so that each line of the text
is no more than 33 characters,
and I want to avoid line breaks
inserted in the middle of
words. I've seen online tools
that add line breaks every X
characters exactly, but this
results in words cut in half. Is
there an easy way to do what
I'm hoping for? If I could just
set the line length on Gmail,
say, to 33 characters and email
myself the text, that would be
what I'm looking for. Thanks!

posted by zengargoyle at 2:34 AM on January 26, 2011 [3 favorites]


Any half-decent text editor will do this for you.

In emacs do M-x set-fill-column to set the line length, and then select your text and do M-x fill-region.
posted by Dr Dracator at 2:51 AM on January 26, 2011 [1 favorite]


Good stuff above, par is a utility I've used on the command line in windows and been happy with.
posted by samj at 2:59 AM on January 26, 2011 [1 favorite]


If you want to do this in Word, I have written a macro that does this at 72 chars (for email purposes) I could post, after minor revisions (stripping out the other things it does, that you don't need). However, I can't guarantee it will work for your version of Word, and Word fucks up text files when it saves them (adding five lines of "???"-garbage at the end).

Lemme know.
posted by IAmBroom at 5:49 PM on January 26, 2011


« Older I have a series of tables desc...   |  IT/Telecom people: I'm bringin... Newer »
This thread is closed to new comments.