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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 35.2 Neatening Text with fmt Chapter 35
You Can't Quite Call This Editing
Next: 35.4 recomment: Clean Up Program Comment Blocks
 

35.3 Alternatives to fmt

fmt ( 35.2 ) is hard to do without once you've learned about it. Unfortunately, it's not available in some versions of UNIX. You can get the GNU version from the CD-ROM. But it's also relatively easy to emulate with sed ( 34.24 ) and nroff ( 43.13 ) . Using those two utilities also lets you take advantage of the more-sophisticated formatting and flexibility that sed and nroff macros ( 43.15 ) can give you. (If you're doing anything really fancy, like tables with tbl ( 43.15 ) , [1] you might need col or colcrt ( 43.18 ) to clean up nroff 's output.)

[1] [The combination of tbl , nroff , and col can make ASCII tables in a few quick steps. The tables aren't sexy, but they can be sophisticated. They can be emailed or printed anywhere and don't require sophisticated viewing equipment. I'm sad that so few people know tbl these days. It's a powerful way to describe tables without worrying about balancing columns or wrapping text in them. And, if you want nicer-looking output, you can feed the same tbl file to groff ( 43.16 ) .  - JP  ]

Here's the script:


#!/bin/sh
sed '1i\
.ll 72\
.na\
.hy 0\
.pl 1' $* | nroff

The reason this is so complicated is that, by default, nroff makes some assumptions you need to change. For example, it assumes an 11-inch page (66 lines), and will add blank lines to a short file (or the end of a long file). The quick-and-dirty workaround to this is to manually put the nroff request .pl 1 (page length 1 line) at the top of the text you want to reformat. nroff also tends to justify lines; you want to turn this off with the .na request. You also want to turn off hyphenation ( .hy 0 ), and you may want to set the line length to 72 instead of nroff 's default 65, if only for consistency with the real fmt program. All these nroff requests get inserted before the first line of input by the sed 1i command.

A fancier script would take a -nn line-length option and turn it into a .ll request for nroff , etc.

- TOR


Previous: 35.2 Neatening Text with fmt UNIX Power Tools Next: 35.4 recomment: Clean Up Program Comment Blocks
35.2 Neatening Text with fmt Book Index 35.4 recomment: Clean Up Program Comment Blocks

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