UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 43.17 Don't Have nroff?  Try gnroff or awf Chapter 43
Printing
Next: 43.19 Removing Leading Tabs and Other Trivia
 

43.18 How nroff Makes Bold and Underline; How to Remove It

The UNIX formatter nroff produces output for line printers and CRT displays. To achieve such special effects as emboldening, it outputs the character followed by a backspace and then outputs the same character again. A sample of it viewed with a text editor or cat -v (25.7) might look like:

N^HN^HN^HNA^HA^HA^HAM^HM^HM^HME^HE^HE^HE

which emboldens the word "NAME." There are three overstrikes for each character output. Similarly, underlining is achieved by outputting an underscore, a backspace, and then the character to be underlined. Some pagers, such as less (25.4), take advantage of overstruck text. But there are many times when it's necessary to strip these special effects; for example, if you want to grep through formatted man pages (as we do in article 50.3). There are a number of ways to get rid of these decorations. The easiest way to do it is to use a utility like col, colcrt, or ul:

Both col and colcrt attempt to handle "half linefeeds" (used to print superscripts and subscripts) reasonably. Many printers handle half linefeeds correctly, but most terminals can't deal with them.

Here's one other solution to the problem: a simple sed (34.24) script. The virtue of this solution is that you can elaborate on it, adding other features that you'd like, or integrating it into larger sed scripts. The following sed command removes the sequences for emboldening and underscoring:

s/.^H//g

It removes any character preceding the backspace along with the backspace itself. In the case of underlining, "." matches the underscore; for emboldening, it matches the overstrike character. Because it is applied repeatedly, multiple occurrences of the overstrike character are removed, leaving a single character for each sequence. Note that ^H is the single character CTRL-h. If you're a vi user, enter this character by typing CTRL-v followed by CTRL-h (31.6). If you're an emacs user, type CTRL-q followed by CTRL-h (32.10).

- DD, ML


Previous: 43.17 Don't Have nroff?  Try gnroff or awf UNIX Power ToolsNext: 43.19 Removing Leading Tabs and Other Trivia
43.17 Don't Have nroff? Try gnroff or awf Book Index43.19 Removing Leading Tabs and Other Trivia

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