[savvyadmin] 0:home1:processes2:recipes3:explain4:archive linux & server reference since 2007
david@web-1:~$ less recipes/line-wrapping-text-made-easy-with-fold.md
LINE-WRAPPING-TEXT-MADE-EASY-WITH-FOLD recipes

Line wrapping text made easy with fold

published 2009-04-06 · tags: cli, linux

AS PUBLISHED, 2009Original text from Apr 06, 2009, kept as published. Package names and flags reflect the tooling of that time; an in-place refresh with a tested on line is on the way.

Line wrapping text from the command line is easy with the fold utility, which of course is provided by the Free Software Foundation.  By default, the fold command will wrap text at 80 characters, but you can of course specify the width manually.  I prefer using the -s option, which will break only on spaces, making sure not to break in the middle of a word.

For example, the following command will concatenate a text file to standard output, adding line breaks  only at spaces or at 72 characters, whichever comes first.

fold -s -w 72 textfile.txt

This can also be useful if you want to clearsign a message with Gnupg, but wish to line wrap it beforehand.

fold -s -w 72 textfile.txt | gpg --clearsign -u user@email.com

Add redirection if you wish to output the results to a file.

fold -s -w 72 textfile.txt > newfile.txt

As mentioned here, the fmt command also provides the same primary features of fold, but is much better. Not only does it wrap long lines, but it also fills out short lines as well. There are additional options that are worth looking into. Be sure to check out the man page!

man fmt
savvyadmin.com 2009-04-06 LINE-WRAPPING-TEXT-MADE-EASY-WITH-FOLD
recipes/line-wrapping-text-made-easy-with-fold.md (END)