fold and fmt

These two commands are useful to split and join lines to meet a specific line length requirement. fmt is smarter and usually the tool you want, but fold can be handy for some cases.

fold

By default, fold will wrap lines that are greater than 80 bytes long, which can be customized using the -w option. The newline character isn't part of this line length calculation. You might wonder if there are tasks where wrapping without context could be useful. One use case I can think of is the FASTA format.

$ cat greeting.txt Hi there Have a nice day # splits the second line since it is greater than 10 bytes $ fold -w10 greeting.txt Hi there Have a nic e day

The -s option looks for the presence of spaces to determine the line splitting. This check is performed within the limits of the wrap length.

$ fold -s -w10 greeting.txt Hi there Have a nice day

However, the -s option can still split words if there's no blank space before the specified width. Use fmt if you don't want this behavior.

$ echo 'hi there' | fold -s -w4 hi ther e

The -b option will cause fold to treat tab, backspace, and carriage return characters as if they were a single byte character.

# tab can occupy up to 8 columns $ printf 'a\tb\tc\t1\t2\t3\n' | fold -w6 a b c 1 2 3 # here, tab will be treated as if it occupies only a single column $ printf 'a\tb\tc\t1\t2\t3\n' | fold -b -w6 a b c 1 2 3

fmt

The fmt command makes a smarter decision based on sentences, paragraphs and other details. Here's an example that splits a single line (taken from the documentation of fmt command) into several lines. The default formatting is 93% of 75 columns. The -w option controls the width parameter and the -g option controls the percentage of columns.

$ fmt info_fmt.txt fmt prefers breaking lines at the end of a sentence, and tries to avoid line breaks after the first word of a sentence or before the last word of a sentence. A sentence break is defined as either the end of a paragraph or a word ending in any of '.?!', followed by two spaces or end of line, ignoring any intervening parentheses or quotes. Like TeX, fmt reads entire "paragraphs" before choosing line breaks; the algorithm is a variant of that given by Donald E. Knuth and Michael F. Plass in "Breaking Paragraphs Into Lines", Software—Practice & Experience 11, 11 (November 1981), 1119–1184.

Unlike the fold command, words are not split even if they exceed the maximum line width. Another difference is that fmt will add a final newline character even if it wasn't present in the input.

$ printf 'hi there' | fmt -w4 hi there

The fmt command also allows you to join lines together that are shorter than the specified width. As mentioned before, paragraphs are taken into consideration, so empty lines will prevent merging. The -s option will disable line merging.

$ cat sample.txt 1) Hello World 2) 3) Hi there 4) How are you 5) 6) Just do-it 7) Believe it 8) 9) banana 10) papaya 11) mango 12) 13) Much ado about nothing 14) He he he 15) Adios amigo # 'cut' here helps to ignore the first 4 characters of sample.txt $ cut -c5- sample.txt | fmt -w30 Hello World Hi there How are you Just do-it Believe it banana papaya mango Much ado about nothing He he he Adios amigo

The -u option will change multiple spaces to a single space. Excess spacing between sentences will be changed to two spaces.

$ printf 'Hi there. Have a nice day\n' | fmt -u Hi there. Have a nice day

There are options that control indentation, formatting only lines with a specific prefix and so on. See fmt documentation for more details.

Exercises

info The exercises directory has all the files used in this section.

1) What's the default wrap length of the fold and fmt commands?

2) Fold the given stdin data at 9 bytes.

$ echo 'hi hello, how are you?' | ##### add your solution here hi hello, how are you?

3) Figure out the logic based on the given input and output data using the fold command.

$ cat ip.txt it is a warm and cozy day listen to what I say go play in the park come back before the sky turns dark There are so many delights to cherish Apple, Banana and Cherry Bread, Butter and Jelly Try them all before you perish ##### add your solution here it is a warm and cozy day listen to what I say

4) What does the fold -b option do?

5) How'd you get the expected output shown below?

# wrong output $ echo 'fig appleseed mango pomegranate' | fold -sw7 fig applese ed mango pomegra nate # expected output $ echo 'fig appleseed mango pomegranate' | ##### add your solution here fig appleseed mango pomegranate

6) What do the options -s and -u of the fmt command do?