Pages

Wrapping lines in a text file to specific width.

fold: Formatting a text file to wrap the lines to specific number of columns.

In case you have a text file that have very long sentences that span long column lengths and you want to wrap the length to specific number of columns then you can make use of the command fold.

For eg let us assume we have a file named one

one:
This is line is very long and  we need to truncate it to lesser number of columns.

The above line approximately spans for around 74 columns. Let us try to wrap it to 50 columns.

$ fold -w 50 one
This is line is very long and  we need to truncate
 it to lesser number of columns.


The number that we pass after the -w option  is the number of columns after which the columns will truncate to.

It is possible that the number of columns specified might split  a word in between.
For eg in the above example if we use 45 instead of 50 the word "truncate" will get split in between.

$ fold  -w 45 one
This is line is very long and  we need to tru
ncate it to lesser number of columns.



To avoid such spliting we can add an option -s , which truncates at the first space before the column width mentioned.
For eg:

$fold -s -w 45 one
This is line is very long and  we need to
truncate it to lesser number of columns.


In the above example we see that with "-s" option the line gets wrapped at the space before the word "truncated".

Happy folding :-)

No comments:

Post a Comment