Linux Command Reference ยท Viewing and Searching Files
Linux nl command
Reference for the Linux nl command: number lines with control over which lines count, width, separator and start value, and how it differs from cat -n.
nl copies its input to standard output and puts a line number in front of each line. Unlike cat -n, it does not number blank lines by default, and it can number only the lines you choose: those matching a regular expression, or all of them.
It also understands "logical pages" with header, body and footer sections, but most people only use the numbering options.
nl [OPTION]... [FILE]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
nl [-b STYLE] [-n FORMAT] [-w WIDTH] [-s SEP] [-v START] [-i INCR] [FILE]...
| Option | Description |
|---|---|
-b STYLE |
Which body lines to number: a all, t non-empty (default), n none, pREGEX only lines matching the regex. |
-n FORMAT |
ln left-justified, rn right-justified (default), rz right-justified with leading zeros. |
-w NUMBER |
Width of the number column (default 6). |
-s STRING |
Text placed between the number and the line (default a tab). |
-v NUMBER |
First line number (default 1). |
-i NUMBER |
Increment between numbers (default 1). |
-p |
Do not restart numbering at each logical page. |
-h STYLE, -f STYLE |
Same as -b, for page headers and footers. |
Returns: 0 on success; non-zero if a file cannot be read.
Usage notes
- Blank lines are skipped, not removed. They stay in the output with an empty number column, so the layout is preserved.
-b pREGEXuses a basic regular expression, written directly afterpwith no space. Quote it so the shell leaves it alone.- Default separator is a tab, so the output can be split with
cutat the first field. - Numbering never wraps. A number wider than
-wsimply widens the column. - Logical pages: lines consisting only of
\:\:\:,\:\:or\:(the delimiters) mark header, body and footer sections. Numbers restart after each page unless-pis given. Ordinary text files never contain these lines. cat -nnumbers every line and uses a fixed format. Usenlwhen you need padding, a start value or selective numbering.- Alternatives:
awk '{ print NR ": " $0 }'gives full control, andgrep -nnumbers only matching lines using their original positions. - Reading standard input: with no file, or
-,nlnumbers what it receives, sols | nlis a quick way to make a pick list.
Related
cat,grep,awk,wc- Lessons: Viewing Text