nl

nl prints a file with its lines numbered. cat -n also numbers lines; nl is the command for when you need control over the numbering — which lines count, how the number looks, where it restarts.

nl [options] [file...]
$ nl chapter.txt
     1  Once the system is installed,
     2  the first boot brings up
       
     3  the configuration service.

Which lines get a number #

By default nl numbers only the non-empty lines. -b sets the rule:

-b STYLENumbers…
-b aevery line.
-b tonly non-empty lines. This is the default.
-b nno lines.
-b pBREonly lines that match the basic regular expression BRE.
OptionEffect
-b, --body-numbering=STYLEThe numbering rule, as above.
-l, --join-blank-lines=NCount a run of N blank lines as one numbered line.

How the number looks #

OptionEffect
-n, --number-format=FORMATln = left-justified; rn = right-justified; rz = right-justified with leading zeros.
-w, --number-width=NUse N columns for the number.
-s, --number-separator=STRINGPut STRING between the number and the line text.
-v, --starting-line-number=NStart counting from N.
-i, --line-increment=NIncrease the number by N at each counted line.

Logical pages #

nl can treat one file as a sequence of logical pages, each with a header, a body, and a footer, and number the three parts by different rules. Pages are separated by special delimiter lines in the input.

OptionEffect
-h, --header-numbering=STYLENumbering style for header sections.
-f, --footer-numbering=STYLENumbering style for footer sections.
-d, --section-delimiter=CCThe characters that mark a section boundary in the input.
-p, --no-renumberDo not reset the line number at the start of each logical page.

The styles for -h and -f are the same a / t / n / pBRE set as -b. For an ordinary file with no delimiter lines, the whole file is one body and only -b matters.

Exit status #

CodeMeaning
0Success.
1A file could not be read, or an option value was invalid.

Edit this page