Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to simply take a given text file and enforce a maximum column length of say 80. How can I turn this:

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

and turn it into this:

Four score and seven years ago our fathers brought forth on this continent, a n
ew nation, conceived in Liberty, and dedicated to the proposition that all men 
are created equal.
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Use the fold command:

fold -s -w 80 filename

Omit the -s option if you prefer to break at character boundaries rather than word boundaries (actually spaces).

I suspect this won't work very well if the text is encoded in a multibyte encoding such as UTF-8 (and contains non-ASCII characters).

If you are using Windows, use Cygwin, UnxUtils or GnuWin32 - they probably include fold.

share|improve this answer
1  
Might want to do fold -s -w 80 filename, so that it breaks at spaces instead of the middle of the word. –  Hyppy Apr 20 '11 at 16:25
    
@Hyppy: Thanks - I'll update the answer to include the -s option. @Gareth: thanks for adding the hyperlinks. –  RedGrittyBrick Apr 20 '11 at 16:50
add comment
$ fmt --width=80 <<< 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'
Four score and seven years ago our fathers brought forth on this continent,
a new nation, conceived in Liberty, and dedicated to the proposition that
all men are created equal.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.