Skip to content

Enhance Line Numbering in Text Files with nl Command

Unleash the power of the nl command to efficiently number lines in text files. Customize numbering styles with ease, including using custom separators, section delimiters, and different numbering formats. Learn to number all lines, only nonempty lines, or lines matching a pattern.


Use NUMBER columns for line numbers

seq 10|nl --number-width=5

Number separator

seq 10 |nl --number-separator='> '

Body numbering style

echo -e "one\ntwo\n\nfour" | nl -b a
nl -h n -f n filename

Use custom section delimiters

echo -e "one\n%\ntwo\n%\nthree" | nl -d '%'

Example usage

find site/incoming/	-type d -iname *linux* |grep -v '_' |sort|sed 's:.*/::'|nl --number-width=4 --number-separator=") "

Number all lines

seq 10 | nl -b a

Number only nonempty lines (default behavior)

echo -e "one\ntwo\n\nfour" | nl -b t

Number no lines

seq 10 | nl -b n

Number only lines that match a regular expression

echo -e "apple\norange\nbanana\ngrape" | nl -b p'ap'

Section delimiter

echo -e "one\n%\ntwo\n%\nthree" | nl -d '%'
echo -e "one\n%\ntwo\n%\nthree" | nl -f a

Header numbering style

echo -e "one\n%\ntwo\n%\nthree" | nl -h a

Line increment

seq 10 | nl -i 2

Join blank lines

echo -e "one\n\n\ntwo\n\n\nthree" | nl -l 2

Number format

Left justified, no leading zeros

seq 10 | nl -n ln

Right justified, no leading zeros (default behavior)

seq 10 | nl -n rn

Right justified, leading zeros

seq 10 | nl -n rz

No renumber

echo -e "one\n%\ntwo\n%\nthree" | nl -p

Number separator

seq 10 | nl -s '-> '

Starting line number

seq 10 | nl -v 5

Number width

seq 10 | nl -w 3

Version information

nl --version