14 Formatting Long Lines


#14 Formatting Long Lines

If you're lucky, your Unix system already includes the fmt command, a program that's remarkably useful if you work with text with any frequency. From reformatting email to filling in paragraphs in documents (that is, making sure that as many words as possible are on each line of the text), fmt is a helpful utility to know.

But some Unix systems don't include fmt , particularly legacy systems at universities, which often have a fairly minimalistic implementation. As it turns out, the nroff command, which has been part of Unix since the very beginning, can be utilized in a short shell script to achieve the same result of wrapping long lines and filling in short lines to even up line lengths.

The Code

 #!/bin/sh # A version of fmt, using nroff. Adds two useful flags: -w X for line width #   and -h to enable hyphenation for better fills. while getopts "hw:" opt; do   case $opt in     h) hyph=1               ;;     w) width="$OPTARG"      ;;   esac done shift $(($OPTIND - 1)) nroff << EOF .ll ${width:-72} .na .hy ${hyph:-0} .pl 1 $(cat "$@") EOF exit 0 

How It Works

This succinct script offers two different command flags, -w X to specify that lines should be wrapped when their width exceeds X characters (the default is 72) and -h to enable hyphenation, filling the lines more and improving the final results. Notice the test to check for starting flags: A while loop uses getopts to step through the options, then uses shift $(($OPTIND - 1)) to throw all the arguments away once they've been processed .

The other, perhaps more important technique demonstrated here is the use of a here document to feed multiple lines of input to a command. The odd double-input-redirect sequence nroff <<EOF allows you to easily have a here document , a section of the script that's treated as if it were typed in on the command line. Using the here document, the script outputs all of the necessary nroff commands and then calls the cat command with the requested filename or filenames to process. The cat command's output is then fed directly to nroff . This is a technique that will appear frequently in the scripts presented in this book, and it's one well worth experimenting with!

Running the Script

This script can be included in a pipe, or it can have filenames specified on the command line, but usually it would be part of an external pipe invoked from within an editor like vi or vim (e.g., !}fmt ) to format a paragraph of text.

The Results

The following example enables hyphenation and specifies a maximum width of 50 characters:

 $  fmt -h -w 50 014-ragged.txt  So she sat on, with closed eyes, and half believed herself in Wonderland, though she knew she had but to open them again, and all would change to dull reality--the grass would be only rustling in the wind, and the pool rippling to the waving of the reeds--the rattling teacups would change to tin- kling sheep-bells, and the Queen's shrill cries to the voice of the shepherd boy--and the sneeze of the baby, the shriek of the Gryphon, and all the other queer noises, would change (she knew) to the confused clamour of the busy farm-yard--while the lowing of the cattle in the distance would take the place of the Mock Turtle's heavy sobs. 

Compare this with the following ouput, generated using the default width and no hyphenation:

 $  fmt 014-ragged.txt  So she sat on, with closed eyes, and half believed herself in Wonderland, though she knew she had but to open them again, and all would change to dull reality--the grass would be only rustling in the wind, and the pool rippling to the waving of the reeds--the rattling teacups would change to tinkling sheep-bells, and the Queen's shrill cries to the voice of the shepherd boy--and the sneeze of the baby, the shriek of the Gryphon, and all the other queer noises, would change (she knew) to the confused clamour of the busy farm-yard--while the lowing of the cattle in the distance would take the place of the Mock Turtle's heavy sobs. 



Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net