3.3 nl: Number lines and write files

nl writes each file (‘-’ means standard input), or standard input if none are given, to standard output, with line numbers added to some or all of the lines. Synopsis:

nl [option]… [file]…

nl decomposes its input into (logical) page sections; by default, the line number is reset to 1 at each logical page section. nl treats all of the input files as a single document; it does not reset line numbers or logical pages between files.

A logical page consists of three sections: header, body, and footer. Any of the sections can be empty. Each can be numbered in a different style from the others.

The beginnings of the sections of logical pages are indicated in the input file by a line containing exactly one of these delimiter strings:

\:\:\:

start of header;

\:\:

start of body;

\:

start of footer.

The characters from which these strings are made can be changed from ‘\’ and ‘:’ via options (see below), but the pattern of each string cannot be changed.

A section delimiter is replaced by an empty line on output. Any text that comes before the first section delimiter string in the input file is considered to be part of a body section, so nl treats a file that contains no section delimiters as a single body section.

The program accepts the following options. Also see Common options.

-b style
--body-numbering=style

Select the numbering style for lines in the body section of each logical page. When a line is not numbered, the current line number is not incremented, but the line number separator character is still prepended to the line. The styles are:

a

number all lines,

t

number only nonempty lines (default for body),

n

do not number lines (default for header and footer),

pbre

number only lines that contain a match for the basic regular expression bre. See Regular Expressions in The GNU Grep Manual.

-d cd
--section-delimiter=cd

Set the section delimiter characters to cd; default is ‘\:’. If only c is given, the second remains ‘:’. As a GNU extension more than two characters can be specified, and also if cd is empty (-d ''), then section matching is disabled. (Remember to protect ‘\’ or other metacharacters from shell expansion with quotes or extra backslashes.)

-f style
--footer-numbering=style

Analogous to --body-numbering.

-h style
--header-numbering=style

Analogous to --body-numbering.

-i number
--line-increment=number

Increment line numbers by number (default 1). number can be negative to decrement.

-l number
--join-blank-lines=number

Consider number (default 1) consecutive empty lines to be one logical line for numbering, and only number the last one. Where fewer than number consecutive empty lines occur, do not number them. An empty line is one that contains no characters, not even spaces or tabs.

-n format
--number-format=format

Select the line numbering format (default is rn):

ln

left justified, no leading zeros;

rn

right justified, no leading zeros;

rz

right justified, leading zeros.

-p
--no-renumber

Do not reset the line number at the start of a logical page.

-s string
--number-separator=string

Separate the line number from the text line in the output with string (default is the TAB character).

-v number
--starting-line-number=number

Set the initial line number on each logical page to number (default 1). The starting number can be negative.

-w number
--number-width=number

Use number characters for line numbers (default 6).

An exit status of zero indicates success, and a nonzero value indicates failure.