nl Command

Purpose

Numbers lines in a file.

Syntax

nl [ -b Type ] [ -f Type ] [ -h Type ] [ -l Number ] [ -d Delimiter ] [ -i Number ] [ -n Format ] [ -v Number ] [ -w Number ] [ -p ] [ -s Separator ] [ File ]

Description

The nl command reads the File parameter (standard input by default), numbers the lines in the input, and writes the numbered lines to standard output. In the output, the nl command numbers the lines on the left according to the flags you specify on the command line.

The input text must be written in logical pages. Each logical page has a header, a body, and a footer section (you can have empty sections). Unless you use the -p flag, the nl command resets the line numbers at the start of each logical page. You can set line-numbering flags independently for the header, body, and footer sections (for example, the header and footer lines can be numbered while the text lines are not).

Signal the start of logical-page sections with lines in the file that contain only the following delimiter characters:

Line Contents Start Of
\:\:\: Header
\:\: Body
\: Footer

You can name only one file on the command line. You can list the flags and the file name in any order.

Flags

All the parameters are set by default. Use the following flags to change these default settings. Except for the -s flag, enter a -n flag without a variable to see its default value.

Item Description
-b Type Chooses which body section lines to number. Recognized values for the Type variable are:
a
Numbers all lines
t
Does not number lines that are blank or lines that contain any non-graphic character such as a tab within them. (default)
n
Does not number any lines
pPattern
Numbers only those lines specified by the Pattern variable.
-d Delimiter Uses the two characters specified by the Delimiter variable as the delimiters for the start of a logical page section. The default characters are \: (backslash, colon). You may specify two ASCII characters, two 1-byte extended characters, or one extended character. If you enter only one 1-byte character after the -d flag, the second character remains the default (a colon). If you want to use a backslash as a delimiter, enter two backslashes (\\).
-f Type Chooses which logical-page footer lines to number. The possible values for the Type variable are the same as the -b flag. The default value of the Type variable is n (no lines numbered).
-h Type Chooses which logical-page header lines to number. The possible values for the Type variable are the same as the -b flag. The default value of the Type variables n (no lines numbered).
-i Number Increments logical-page line numbers by the number specified in the Number variable. The default value of the Number variable is 1. The range of the Number variable is from 1 to 250.
-l Number (Lowercase L) Uses the value specified in the Number parameter as the number of blank lines to count as one. For example, -l3 numbers every third blank line in a series. The default value of the Number variable is 1. This flag works when the -ha, -ba, or -fa option is set. The range of the Number variable is from 1 to 250.
-n Format Uses the value of the Format variable as the line numbering format. Recognized formats are:
ln
Left-justified, leading zeros suppressed
rn
Right-justified, leading zeros suppressed (default)
rz
Right-justified, leading zeros kept
-p Does not restart numbering at logical page delimiters.
-s Separator Separates the text from its line number with the character specified in the Separator variable. The default value of the Separator variable is a tab character.
-v Number Sets the initial logical-page line number to the value specified by the Number variable. The default value of the Number variable is 1. The range of the Number variable is from 0 to 32767.
-w Number Uses the value specified by the Number variable as the number of characters in the line number. The default value of the Number variable is 6. The range of the Number variable is from 1 to 20.

Exit Status

This command returns the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. To number only the non-blank lines, enter:
    nl  chap1
    This displays a numbered listing of chap1, numbering only the non-blank lines in the body sections. If chap1 contains no \:\:\+:, or \: delimiters, then the entire file is considered the body.
  2. To number all lines:
    nl  -ba  chap1
    This numbers all the lines in the body sections, including blank lines. This form of the nl command is adequate for most uses.
  3. To specify a different line number format, enter:
    nl  -i10  -nrz  -s::  -v10  -w4  chap1
    This numbers the lines of chap1 starting with ten (-v10) and counting by tens (-i10). It displays four digits for each number (-w4), including leading zeros (-nrz). The line numbers are separated from the text by two colons (-s : :).

    For example, if chap1 contains the text:

    A  not-so-important
    note  to  remember:
     
    You  can't  kill  time
    without  injuring  eternity.
    then the numbered listing is:
    0010::A  not-so-important
    0020::note  to  remember
     
    0030::You  can't  kill  time
    0040::without  injuring  eternity.
    Note that the blank line was not numbered. To do this, use the -ba flag as shown in example 2.

Files

Item Description
/usr/bin/nl Contains the nl command.