nl — Number lines in a file

Format

nl [–btype] [–dxy] [–ftype] [–htype] [–in] [–ln] [–nfmt] [–p] [–ssep] [–v[n]] [–w[n]] [file]

Description

nl is a filter that numbers lines in a single file. If you do not specify file on the command line, the standard input is used.

The input is displayed as a stream of text lines, possibly divided into logical pages by separators. In turn, each page consists of a header, body, or footer, in that order. Any missing part is assumed to be empty. Using the default page delimiter character of \ and :, lines consisting entirely of the following combinations are logical page part delimiters and are not numbered.
Input line
Starts
\:\:\:
Page header
\:\:
Page body
\:
Page footer

Options

–btype
Specifies the numbering type for each page body. The numbering type is one of the following options:
a
Numbers all lines
n
Does not number any lines
pregexp
Numbers only those lines that contain the basic regular expression regexp. See Regular expressions (regexp) for more information about regexp.
t
Numbers only those lines that are not empty. An empty line consists of only a newline character.
The default body numbering type is t.
–dxy
Changes the default delimiter characters to characters x and y. If only x is specified, only the first delimiter character is changed. The default delimiter characters are \ and :.
–ftype
Specifies the page footer numbering type (see the –b option). The default type is n.
–htype
Specifies the page header numbering type (see the –b option). The default type is n. (The lines are not numbered.)
–in
Sets the line increment to n rather than the default value of l.
–ln
When the page numbering type is (all), blank lines are treated specially. Every nth consecutive blank line is numbered. If you do not specify this option, n defaults to 1 and every blank line is numbered.
–nfmt
Specifies the line numbering format, which must be one of the following:
n
Right-aligned line number, padded to width (see –w) on the left with spaces (the default format).
rz
Right-aligned line number, padded on left with zeroes.
ln
Left-aligned line number, padded on right with spaces.
–p
Specifies continuous page numbering across page boundaries. By default, nl restarts numbering (as in the next option) at each new page.
–ssep
The string sep is printed to separate the line number from the text of the line being numbered. When this option is not specified, this separator is a single tab character.
–vn
Starts numbering for each new page at n. If you do not specify this option, page numbering starts at 1.
–wn
Sets the width of the line number in the output to n. If you do not specify n, the default is 6.

Examples

The following command numbers every second consecutive blank line, using page delimiters of ~!:
nl –l2 –ha –ba –fa –n  rz –v10 –i10 –d~! file

Localization

nl uses the following localization environment variable:
  • LC_SYNTAX
  • NLSPATH

See Localization for more information.

Messages

0
Successful completion
1
Failure due to any of the following:
  • Incorrect command-line argument
  • More than one file name was specified
  • Unable to open the file
  • Incorrect regular expression in –b, –f, –h
  • Incorrect numbering type
  • Badly formed number in a command-line option

Portability

POSIX.2, X/Open Portability Guide.

Related information

awk, pr

See Regular expressions (regexp) for more information about regexp.