Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .