Before you print a file on a UNIX system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not look quite as nice.
Many versions of UNIX include two powerful text formatters, nroff and troff . (There are also versions called gnroff and groff .) They are much too complex to describe here. Before we cover printing itself, let's look at a simple formatting program called pr .
The syntax is:
pr changes the format of the file only on the screen or on the printed copy; it doesn't modify the original file. Table 4.2 lists some pr options.
Other options allow you to specify the width of the columns, set the page length, and so on.
Before using pr , here are the contents of a sample file named food :
Let's use pr options to make a two-column report with the header "Restaurants."
The text is output in two-column pages. The top of each page has the date and time, header (or name of the file, if header is not supplied), and page number. To send this output to the printer instead of the terminal screen, you create a pipe to the printer program - usually lp or lpr . The following section describes lp and lpr ; Chapter 5 covers pipes.
If you have a long file, it may be best to print it so you can see it all on paper. The command lp or lpr prints a file (onto paper as opposed to the screen display). Your system will probably have one or the other - but not both. The syntax is:
Printers on UNIX systems are usually shared by a group of users. After you enter the command to print a file, the shell prompt returns to the screen and you can enter another command. However, seeing the prompt doesn't mean that your file has been printed. Your file has been added to the printer queue to be printed in turn.
Your system administrator has probably set up a default printer at your site. To print a file named bills on the default printer, use the lp or lpr command, as in this example:
lp shows an ID that you can use to cancel the print job or check its status. If you need ID numbers for lpr jobs, use the lpq command (see "lpstat and lpq" in the following section). The file bills will be sent to a printer called laserp . The ID number of the request is "laserp-525".
lp and lpr have several options. Table 4.3 lists three of them.
If lp and lpr don't work at your site, ask other users for the printer command. You'll also need the printer locations - so you know where to get your output.
If you want to find out how many files or "requests" for output are ahead of yours in the printer queue, use the command lpstat (for lp ) or lpq (for lpr ). The cancel command lets you terminate a printing request made by lp ; lprm cancels jobs from lpr .
The lpstat command shows what's in the printer queue: request IDs, owners, file sizes, when the jobs were sent for printing, and the status of the requests. Use lpstat -o if you want to see all output requests rather than just your own. Requests are shown in the order they'll be printed:
The first entry shows that the request "laserp-573" is currently printing on laserp . The exact format and amount of information given about the printer queue may differ from system to system. If the printer queue is empty, lpstat will say "No entries" or simply give you back the shell prompt.
The first line displays the printer status. If the printer is disabled or out of paper, you may see different messages on this first line. The "active" job, the one being printed, is listed first. The "Job" number is like the lpstat request ID. To specify another printer, and the P option ( Table 4.3 ).
If you don't have the request ID, get it from lpstat or lpq . Then use cancel or lprm . Specifying the request ID cancels the request, even if it is currently printing:
To cancel whatever request is currently printing, regardless of its ID, simply enter cancel and the printer name:
command will cancel the active job if it belongs to
you. Otherwise, you can give job numbers as arguments, or use a dash
lprm tells you the actual filenames removed from the printer queue (which you probably don't need).
In this exercise, you'll create, rename and delete files. Find out if your site has one or more printers as well as the appropriate command to use for printing.