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
Don't number header and footer lines
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 only nonempty lines (default behavior)
echo -e "one\ntwo\n\nfour" | nl -b t
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 '%'
Footer numbering style
echo -e "one\n%\ntwo\n%\nthree" | nl -f a
Header numbering style
echo -e "one\n%\ntwo\n%\nthree" | nl -h a
Join blank lines
echo -e "one\n\n\ntwo\n\n\nthree" | nl -l 2
Left justified, no leading zeros
Right justified, no leading zeros (default behavior)
Right justified, leading zeros
No renumber
echo -e "one\n%\ntwo\n%\nthree" | nl -p