|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| News | Syntax | Recommended Links | Sorting algorithms | Recommended Papers | Rcut | Reference | Pipes |
| Perl re-implemenations | uniq | sort | tr | AWK | Tips | Humor | Etc |
The external fold command is a filter that folds lines longer than 80 characters into lines less than 80 characters. You can specify the width of the line instead of using the default of 80.
You use fold to break long lines down into shorter more readable lines. This is a very useful function if you have a terminal that does not wrap its lines onto the next line. It may also be used for printers that do not know how to wrap lines longer than a certain width onto the next line. By using fold you can perform the wrapping function before the data reaches the output device.
Some terminals automatically wrap long output lines onto the next line. This looks good but may cause problems with commands that attempt to count displayed lines. If the line is longer than 80 characters, it takes up two lines of your terminal screen. Programs like pr format the output based on the number of physical lines read. Thus a line may scroll off the top of your screen or your printed output may not be aligned on the paper correctly, because the number of physical lines does not match the number of displayed lines.
The fold command may be used to correct this problem, because it breaks the long lines down into shorter physical lines, making other commands count the proper number of physical lines.
If tabs are present in the input data, you may want to send the data through expand first, then pipe it into fold.
COMMAND FORMAT
Following is the general format of the fold command.
fold [ -wwidth ] [ file_list ]
fold [ -width ] [ file_list ]
The following option and its argument may be used to control how fold functions.
| -width -wwidth | Specifies the width of the output line in characters. The default is 80. The width should always be a multiple of your tab length if tabs exist within the input data. Usually tabs are set every eight spaces. |
The following argument may be passed to the fold command.
| file_list | A list of files to be read and checked for lines longer than the specified or default width. |
Refer to the expand command described in Module 46.
In this activity you use the fold command to wrap long lines onto the next output line. First you must create a file with lines longer than 80 characters. Begin at the shell prompt.
cj> vi xwide This is a line of text that is longer than 80 characters. It wraps around to the next line on some terminals but not on others. Therefore we use fold.<CR> Another line just for filling up space. The fold command cuts these lines off at 80 characters (or there about) and displays the remainder of the line on the next output line.<Escape>
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: September 15, 2008