Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that I can use something like cat test.txt | pr -w 80 to wrap lines to 80 characters wide, but that puts a lot of space on the top and bottom of the printed lines and it does not work right on some systems

What's the best way to force a text file with long lines to be wrapped at a certain width?

Bonus points if you can keep it from breaking words.

share|improve this question

4 Answers 4

up vote 29 down vote accepted

You are looking for

fold text.txt -w 80 -s
  • -w tells the width of the text, where 80 is standard.
  • -s tells to break at spaces, and not in words.

That's the way it's called on debian/ubuntu there are other systems, which need "-c" instead of "-w".

share|improve this answer
    
Works on OS X, too, but filename needs to be after args. Thanks! –  rdrey Sep 2 at 22:13

In addition to fold, take a look at fmt. fmt tries to choose line breaks intelligently to make text look good. It will also join adjacent lines, which is good for prose but bad for log files or other formatted text.

share|improve this answer
1  
I especially like fmt -t compared to fold –  lkraav Dec 24 '12 at 21:26

Another (less known) tool that does what you want is wrap from GNU Talkfilters:

wrap -w 80 < textfile

Also (off topic):

but that puts a lot of space on the top and bottom of the printed lines

add -t when invoking pr to omit headers/trailers:

   -t, --omit-header
          omit page headers and trailers
share|improve this answer

And for more formatting options, look at par -- http://www.nicemice.net/par/

share|improve this answer

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.