Classic Shell Scripting - Arnold Robbins [53]
$ sed -n -e 19000,19025p /usr/dict/words | pr -c5 -t
reproach repugnant request reredos resemblant
reptile repulsion require rerouted resemble
reptilian repulsive requisite rerouting resent
republic reputation requisition rescind resentful
republican repute requited rescue reserpine
repudiate
If the column width is too small, pr silently truncates data to prevent column overlap. We can format the same 26 words into 10 (truncated) columns like this:
$ sed -n -e 19000,19025p /usr/dict/words | pr -c10 -t
reproa republ repugn reputa requir requit rerout rescue resemb resent
reptil republ repuls repute requis reredo rescin resemb resent reserp
reptil repudi repuls reques requis rerout
pr has a lot of options, and historically, there was considerable variation among Unix systems in those options, and in the output format and number of lines per page. We recommend using the version from the GNU coreutils package, since it gives a uniform interface everywhere, and more options than most other versions. Consult the manual pages for pr(1) for the details.
Although some PostScript printers accept plain text, many do not. Typesetting systems like TEX and troff can turn marked-up documents into PostScript and/or PDF page images. If you have just a plain text file, how do you print it? The Unix printing system invokes suitable filters to do the conversion for you, but you then do not have any control over its appearance. The answer is text-to-PostScript filters like a2ps,[5] lptops,[6] or on Sun Solaris only, mp. Use them like this:
a2ps file > file.ps Make a PostScript listing of file
a2ps file | lp Print a PostScript listing of file
lptops file > file.ps Make a PostScript listing of file
lptops file | lp Print a PostScript listing of file
mp file > file.ps Make a PostScript listing of file
mp file | lp Print a PostScript listing of file
All three have command-line options to choose the font, specify the typesize, supply or suppress page headers, and select multicolumn output.
BSD, IBM AIX, and Sun Solaris systems have vgrind,[7] which filters files in a variety of programming languages, turning them into troff input, with comments in italics, keywords in bold, and the current function noted in the margin; that data is then typeset and output as PostScript. A derivative called tgrind [8] does a similar job, but with more font choices, line numbering, indexing, and support for many more programming languages. tgrind produces TEX input that readily leads to PostScript and PDF output. Figure 4-1 shows a sample of its output. Both programs are easy to use for printing of typeset program listings:
$ tgrind -p hello.c
Typeset and print hello.c
$ tgrind -i 1 -fn Bookman -p hello.c
Print the listing shown in Figure 4-1
$ vgrind hello.c | lp
Typeset and print hello.c
Figure 4-1. tgrind typesetting of a famous C program
* * *
[2] A daemon (pronounced dee-mon) is a long-running process that provides a service, such as accounting, file access, login, network connection, printing, or time of day.
[3] Available at http://www.cups.org/ and documented in a book listed in the Chapter 16.
[4] Available at http://www.lprng.org/.
[5] Available at ftp://ftp.gnu.org/gnu/a2ps/.
[6] Available at http://www.math.utah.edu/pub/lptops/.
[7] Available at http://www.math.utah.edu/pub/vgrind/.
[8] Available at http://www.math.utah.edu/pub/tgrind/.
Extracting the First and Last Lines
It is sometimes useful to extract just a few lines from a text file—most commonly, lines near the beginning or the end. For example, the chapter titles for the XML files for this book are all visible in the first half-dozen lines of each file, and a peek at the end of job-log files provides a summary of recent activity.
Both of these operations are easy. You can display the first n records of standard input or each of a list of command-line files with any of these:
head -n n [ file(s)