Linux nl command
Updated: 03/13/2021 by Computer Hope
On Unix-like operating systems, the nl command numbers the lines in a file.
This page describes the GNU/Linux version of nl.
Syntax
nl [OPTION]... [FILE]...
Options
-b, --body-numbering=STYLE |
use STYLE for numbering body lines |
-d, --section-delimiter=CC |
use CC for separating logical pages |
-f, --footer-numbering=STYLE |
use STYLE for numbering footer lines |
-h, --header-numbering=STYLE |
use STYLE for numbering header lines |
-i, --line-increment=NUMBER |
line number increment at each line |
-l, --join-blank-lines=NUMBER |
group of NUMBER empty lines counted as one |
-n, --number-format=FORMAT |
insert line numbers according to FORMAT |
-p, --no-renumber |
do not reset line numbers at logical pages |
-s, --number-separator=STRING |
add STRING after (possible) line number |
-v, --starting-line-number=NUMBER |
first line number on each logical page |
-w, --number-width=NUMBER |
use NUMBER columns for line numbers |
--help | display help and exit |
--version | display version information and exit |
By default, nl selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are two delimiter characters for separating logical pages. A missing second character implies a colon (:).
For a backslash (\), two backslashes (\\).
STYLE is one of:
a | number all lines |
t | number only nonempty lines |
n | number no lines |
pBRE | number only lines containing a match for the basic regular expression, BRE |
FORMAT is one of:
ln | left justified, no leading zeros |
rn | right justified, no leading zeros |
rz | right justified, leading zeros |
Examples
cat list.txt
apples oranges potatoes lemons garlic
nl list.txt
1 apples 2 oranges 3 potatoes 4 lemons 5 garlic
In the example above, we use the cat command to display the contents of list.txt. Then we use nl to number each line and display the result to standard output.
nl list.txt > nlist.txt
cat nlist.txt
1 apples 2 oranges 3 potatoes 4 lemons 5 garlic
In the example above, we run the same nl command, but redirect the output to a new file, nlist.txt. Then we use cat to display the results.
Related commands
wc — Display a count of lines, words, and characters in a file.