nl
Add line numbers to text files
Syntax
nl [options] [file]
Basic Usage
Number lines in a file
nl filename.txt
Adds line numbers to the specified file.
Number lines from stdin
cat filename.txt | nl
Number lines with custom starting number
nl -v 10 filename.txt
Common Options
-b style: Body numbering style (a=all, t=non-empty, n=none, p=regex)-f style: Footer numbering style-h style: Header numbering style-i number: Increment line numbers by specified value-l number: Join blank lines (default is 1)-n format: Number format (ln=left, rn=right, rz=right with zeros)-p: Don't reset line numbers at logical page breaks-s string: String to separate line numbers from text-v number: Starting line number-w number: Width of line number field
Practical Examples
Basic line numbering
nl document.txt
Number only non-empty lines
nl -b t document.txt
Custom separator and width
nl -s " | " -w 3 document.txt
Right-aligned numbers with zeros
nl -n rz -w 4 document.txt
Start numbering from 100
nl -v 100 document.txt
Increment by 5
nl -i 5 document.txt
Number with custom format
nl -s " --> " -w 2 -n ln document.txt
Number only specific sections
nl -b p'^[A-Z]' document.txt
Numbering Styles
Body Numbering (-b)
a: Number all linest: Number only non-empty linesn: No numberingp regex: Number only lines matching regex
Number Format (-n)
ln: Left-justified, no leading zerosrn: Right-justified, no leading zerosrz: Right-justified, with leading zeros
Best Practices
When to Use nl
- Adding line numbers to code files for reference
- Numbering document lines for easy citation
- Creating numbered lists from text files
- Preparing text for documentation or reports
- Debugging and troubleshooting text processing
Important Notes
- nl modifies the output but doesn't change the original file
- Use redirection (>) to save numbered output to a new file
- Blank lines are counted by default unless using -b t
- Line numbers are left-aligned by default
- Consider using cat -n for simpler line numbering