2009年4月12日星期日

Line wrapping text made easy with fold

Line wrapping text made easy with fold

Posted on April 6th, 2009 in Tech Tips by gmendoza

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