home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 35.1 And Why Not? Chapter 35
You Can't Quite Call This Editing
Next: 35.3 Alternatives to fmt

35.2 Neatening Text with fmt

One of the problems with fold (43.8 ) is that it breaks text at an arbitrary column position - even if that position happens to be in the middle of a word. It's a pretty primitive utility, designed to keep long lines from printing off the edge of a line printer page, and not much more.

fmt can do a better job because it thinks in terms of language constructs like paragraphs. fmt wraps lines continuously, rather than just folding the long ones. It assumes that paragraphs end at blank lines.

You can use fmt for things like neatening lines of a mail message or a file that you're editing with vi (30.37 ) . (Emacs has its own built-in line-neatener.) It's also great for shell programming and almost any place you have lines that are too long or too short for your screen.

NOTE: On at least one version of UNIX, fmt is a disk initializer (disk formatter) command. Don't run that command accidentally! Check your online manual page and see the fmt equivalents below.

There are a few different versions of fmt , some fancier than others. In general, the program assumes that:

  • Paragraphs have blank lines between them.

  • If a line is indented, keep the indentation.

  • The output lines should be about 70 characters wide. Some have a command line option to let you set this. For example, fmt -132 (or, on some versions, fmt -l 132 ) would reformat your file to have lines with no more than 132 characters on each.

  • It reads files or standard input. Lines will be written to standard output.

The GNU fmt is on the CD-ROM. There are also a couple of freely available versions. Many versions of fmt have options for other structured data. The -p option (35.4 ) reformats program source code. (If your fmt doesn't have -p , the recomment (35.4 ) script uses standard fmt with sed to do the same thing.) The -s option breaks long lines at whitespace but doesn't join short lines to form longer ones.

Alternatively, you can make your own (35.3 ) simple (and a little slower) version with sed and nroff . If you want to get fancy (and use some nroff and/or tbl coding), this will let you do automatically formatted text tables, bulleted lists, and much more.

- JP , TOR

Previous: 35.1 And Why Not? UNIX Power Tools Next: 35.3 Alternatives to fmt
35.1 And Why Not? Book Index 35.3 Alternatives to fmt

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System