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 STYLE | Numbers… |
|---|---|
-b a | every line. |
-b t | only non-empty lines. This is the default. |
-b n | no lines. |
-b pBRE | only lines that match the basic regular expression BRE. |
| Option | Effect |
|---|---|
-b, --body-numbering=STYLE | The numbering rule, as above. |
-l, --join-blank-lines=N | Count a run of N blank lines as one numbered line. |
How the number looks #
| Option | Effect |
|---|---|
-n, --number-format=FORMAT | ln = left-justified; rn = right-justified; rz = right-justified with leading zeros. |
-w, --number-width=N | Use N columns for the number. |
-s, --number-separator=STRING | Put STRING between the number and the line text. |
-v, --starting-line-number=N | Start counting from N. |
-i, --line-increment=N | Increase 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.
| Option | Effect |
|---|---|
-h, --header-numbering=STYLE | Numbering style for header sections. |
-f, --footer-numbering=STYLE | Numbering style for footer sections. |
-d, --section-delimiter=CC | The characters that mark a section boundary in the input. |
-p, --no-renumber | Do 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 #
| Code | Meaning |
|---|---|
0 | Success. |
1 | A file could not be read, or an option value was invalid. |