The nl command reads lines from a file or the standard input and writes them on the standard output with line numbering.
nl views the text it reads in terms of logical pages. A logical page consists of a header, a body, and a footer section. Empty sections are valid.
The start of a header, body, and footer of a logical page is normally signaled by an input line containing nothing but one of the following strings:
| for start of header |
| for start of body |
| for start of footer |
If the input text does not contain any delimiter characters, nl assumes that the text being read is in a single logical page body.
Line numbering is reset at the start of a logical page (exception: option -p). Different line numbering options are independently available for header, body, and footer (e.g. no numbering of header and footer lines, while numbering blank lines only in the body).
Syntax
nl[ option]...[ file] |
No option specified nl numbers all logical page body lines that contain printable text, but not header and footer lines. Line numbering is reset to 1 at the start of each logical page. Within a logical page, nl numbers lines in increments of 1. Each line number may be up to 6 positions long and is output right justified without leading zeros. A tab is used to separate line numbers from text. option The options must be entered individually, i.e. separated from other options by blanks. The name of the file may be specified before, between, or after the options. The position of the file name in the command line has no effect on the operation of the nl command. |
Selecting the lines to be numbered
(b -body) Specifies which logical page body lines are to be numbered. type can be any of: a, n, preg_expr, or t.
All lines are numbered.
No lines are numbered.
Numbers all lines containing strings that match the given reg_expr. reg_expr is a simple regular expression (see Tables and Dirctories, Regular POSIX shell expressions). If the specified regular expression contains shell metacharacters, it must be enclosed in single quotes:
All lines with printable text are numbered. -b not specified:
(f- footer) Specifies which logical page footer lines are to be numbered. type may be: a, n, preg_expr, or t. -f not specified:
(h - header) Specifies which logical page header lines are to be numbered. type may be: a, n, preg_expr, or t. -h not specified: |
Restarting line numbering
Line numbering is not restarted at logical page delimiters.
Line numbering is restarted at startnum at the start of each logical page. -v not specified: |
Defining the increment
incr is the increment value used to number logical page lines. -i not specified: |
Defining the output format
format is the line numbering format. The recognized values are: ln, rn, or rz.
Prints line numbers left justified, suppressing leading zeros.
Prints line numbers right justified, suppressing leading zeros.
Prints line numbers right justified, but with leading zeros. -n not specified:
Defines sep as the separator between line numbers and the corresponding text lines. sep may be one or more characters. -s not specified:
Defines n as the number of positions for line numbers. -w not specified: |
Numbering blank lines
nl treats n consecutive blank lines as a single blank line. Example
results in only every second blank line being numbered when there are several consecutive blank lines (none of the header and footer lines are numbered). -l not specified: |
Defining delimiters for header, body and footer
Changes the delimiter characters that specify the start of a header, body or footer section from \: to the string xy. If a backslash (\) is to be specified for x or y, it must be escaped by single quotes or a second backslash, e.g. -d’\*’ or -d\\*. y not specified: |
Name of the input file. file not specified: |
Locale
LANG
Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
LC_ALL
If set to a non-empty string value, override the values of all the other internationalization variables.
LC_COLLATE
Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions.
LC_CTYPE
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the behavior of character classes within regular expressions, and for deciding which characters are in a certain character class for the -b, -f and -h options.
LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
NLSPATH
Determine the location of message catalogs for the processing of LC_MESSAGES.
Example
The file poems has the following contents: \:\:\: Taken from: Do you love me? \:\: The trouble with you 's you've lost a screw I'm sorry it's you but there's nothing to do There'll be no abatements there are no replacements don't make a to-do just say toodle-oo I'm sorry I can't help you you'd cost too much to redo you'll have to be abolished report to be demolished \: Ronald Laing \:\:\: Limerick \:\: A limerick packs laughs anatomical into space that is short and economical; But the good ones I've seen so seldom are clean, And the clean ones so seldom are comical. \: Author unknown Call nl without options
Number lines in increments of 10
|
See also
ed, pr |