LPIC-1 Linux Professional Institute Certification Study Guide. Richard Blum
Читать онлайн книгу.-w
width, and -width=
width options, which set the line length to width characters.
Numbering Lines with nl
As described earlier, in “Combining Files with cat
,” you can number the lines of a file with that command. The cat
line-numbering options are limited, though, if you need to do complex line numbering. The nl
command is the tool to use in this case. In its simplest form, you can use nl
alone to accomplish much the same goal as cat – b
achieves: numbering all the non-blank lines in a file. You can add many options to nl
to achieve various special effects:
Body Numbering Style
You can set the numbering style for the bulk of the lines with the -b
style or -body-numbering=
style option, where style is a style format code, described shortly.
Header and Footer Numbering Style
If the text is formatted for printing and has headers or footers, you can set the style for these elements with the -h
style or -header-numbering=
style option for the header and -f
style or -footer-numbering=
style option for the footer.
Page Separator
Some numbering schemes reset the line numbers for each page. You can tell nl
how to identify a new page with the -d=
code or -section-delimiter=
code option, where code is a code for the character that identifies the new page.
Line-Number Options for New Pages
Ordinarily, nl
begins numbering each new page with line 1. If you pass the -p
or -no-renumber
option, though, it doesn't reset the line number with a new page.
Number Format
You can specify the numbering format with the -n
format or -number-format=
format option, where format is ln
(left justified, no leading zeros), rn
(right justified, no leading zeros), or rz
(right justified with leading zeros).
The body, header, and footer options enable you to specify a numbering style for each of these page elements, as described in Table 1.3.
Table 1.3 Styles used by nl
As an example, suppose you've created a script, buggy
, but you find that it's not working as you expect. When you run it, you get error messages that refer to line numbers, so you want to create a version of the script with lines that are numbered for easy reference. You can do so by calling nl
with the option to number all lines, including blank lines (-b a
):
Because the input file doesn't have any explicit page delimiters, the output will be numbered in a single sequence. The nl
command doesn't try to impose its own page-length limits.
The numbered-buggy.txt
file created by this command isn't useful as a script because of the line numbers that begin each line. You can, however, load it into a text editor or display it with a pager such as less
to view the text and see the line numbers along with the commands they contain.
Preparing a File for Printing with pr
If you want to print a plain-text file, you may want to prepare it with headers, footers, page breaks, and so on. The pr
command was designed to do this. In its most basic form, you pass the command a file:
The result is text formatted for printing on a line printer – that is, pr
assumes an 80-character line length in a monospaced font. Of course, you can also use pr
in a pipe, either to accept input piped from another program or to pipe its output to another program. (The recipient program might be lpr
, which is used to print files, as described in Chapter 6, “Configuring the X Window System, Localization, and Printing.”)
By default, pr
creates output that includes the original text with headers, which lists the current date and time, the original filename, and the page number. You can tweak the output format in a variety of ways, including the following:
Generate Multicolumn Output
Passing the -
numcols or -columns=
numcols option creates output with numcols columns. For example, if you typed pr -3 myfile.txt, the output would be displayed in three columns. Note that pr
doesn't reformat text; if lines are too long, they're truncated or run over into multiple columns.
Generate Double-Spaced Output
The -d
or -double-space
option causes double-spaced output from a single-spaced file.
Use Form Feeds
Ordinarily, pr
separates pages by using a fixed number of blank lines. This works fine if your printer uses the same number of lines that pr
expects. If you have problems with this issue, you can pass the -F
, -f
, or -form-feed
option, which causes pr
to output a form-feed character between pages. This works better with some printers.
Set Page Length
The -l
lines or -length=
lines option sets the length of the page in lines.
Set the Header Text
The -h
text or -header=
text option sets the text to be displayed in the header, replacing the filename. To specify a multi-word string, enclose it in quotes, as in -header=”My File”
. The -t
or -omit-header
option omits the header entirely.
Set Left Margin and Page Width
The -o
chars or -indent=
chars option sets the left margin to chars characters. This margin size is added to the page width, which defaults to 72 characters and can be explicitly set with the -w
chars or -width
chars option.
These options are just the beginning; pr
supports many more options, which are described in its man
page. As an example of pr
in action, consider printing a double-spaced and numbered version of a configuration file (say, /etc/profile
) for your reference. You can do this by piping together cat
and its -n
option to generate a numbered output, pr
and its -d
option to double-space the result, and lpr
to print the file:
The result should be a printout that might be handy for taking notes on the configuration file. One caveat, though: If the file contains lines that approach or exceed 80 characters in length, the result can be single lines that spill across two lines. The result will be disrupted page boundaries. As a workaround, you can set a somewhat short page length with -l
and use -f
to ensure that the printer receives form feeds after each page:
The pr
command is built around assumptions about printer capabilities that were reasonable in the early 1980s. It's still useful today, but you might prefer to look into GNU Enscript (www.codento.com/people/mtr/genscript/). This program has