4.5. Printing FilesBefore 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. 4.5.1. prThe pr program does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns. The syntax is: pr option(s) filename(s) 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-1 lists some pr options. Table 4-1. Some pr options
Other options allow you to specify the width of columns, set the page length, etc. Before using pr, here are the contents of a sample file named food:
$ cat food Sweet Tooth Bangkok Wok Mandalay Afghani Cuisine Isle of Java Big Apple Deli Sushi and Sashimi Tio Pepe's Peppers . . . Let's use pr options to make a two-column report with the header "Restaurants":
$ pr -2 -h "Restaurants" food Oct 6 9:58 2001 Restaurants Page 1 Sweet Tooth Isle of Java Bangkok Wok Big Apple Deli Mandalay Sushi and Sashimi Afghani Cuisine Tio Pepe's Peppers . . . $ 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, create a pipe to the printer program--usually lp or lpr. The following section describes lp and lpr; Chapter 5 covers pipes. 4.5.2. lp and lprThe command lp or lpr prints a file (onto paper as opposed to the screen). Some systems have lp; others have lpr. The syntax is: lp option(s) filename(s) lpr option(s) filename(s) 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 bills request id is laserp-525 (1 file) $ 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 program (see Section 4.5.3.1 later in this chapter). 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-2 lists three of them. Table 4-2. Some lp and lpr options
Windowing applications like StarOffice typically run lp or lpr for you, "behind the scenes." They may have a printer configuration menu entry where you can specify any lp or lpr options you want to use on every print job. 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. 4.5.2.1. Problem checklist
4.5.3. Viewing the Printer QueueIf you want to find out how many files or "requests" for output are ahead of yours in the printer queue, use the program named lpstat (for lp) or lpq (for lpr). The cancel program lets you terminate a printing request made by lp; lprm cancels jobs from lpr. If you have a graphical application such as StarOffice that does its printing with lp or lpr, you should be able to use these commands to check and cancel those print jobs. 4.5.3.1. lpstat and lpqThe lpstat program 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:
$ lpstat -o laserp-573 john 128865 Oct 6 11:27 on laserp laserp-574 grace 82744 Oct 6 11:28 laserp-575 john 23347 Oct 6 11:35 $ 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 says "No entries" or simply gives you back the shell prompt. lpq gives slightly different information than lpstat -o:
$ lpq laserp is ready and printing Rank Owner Job Files Total Size active john 573 report.ps 128865 bytes 1st grace 574 ch03.ps ch04.ps 82744 bytes 2nd john 575 standard input 23347 bytes $ 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, add the -P option (Table 4-2). 4.5.3.2. cancel and lprmcancel terminates a printing request from the lp program. lprm terminates lpr requests. You can specify either the ID of the request (displayed by lp or lpq) or the name of the printer. 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:
$ cancel laserp-575 request "laserp-575" cancelled To cancel whatever request is currently printing, regardless of its ID, simply enter cancel and the printer name:
$ cancel laserp request "laserp-573" cancelled The lprm program will cancel the active job if it belongs to you. Otherwise, you can give job numbers as arguments, or use a dash (-) to remove all of your jobs:
$ lprm 575 dfA575diamond dequeued cfA575diamond dequeued lprm tells you the actual filenames removed from the printer queue (which you probably don't need). 4.5.3.3. Exercise: manipulating filesIn this exercise, you'll create, rename, and delete files. First you'll need to find out if your site has one or more printers and the appropriate command to use for printing.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|