Linux nl Command
1. nl Command Overview
nlIt is a simple but practical command-line tool in the Linux system, with its full name being "number lines" (line numbering). Its main function is to read the file content, add a line number to each line, and then output the result to standard output.
1.1 Why Do We Need the nl Command?
In daily development or system administration, we often need to:
- Quickly view specific lines of code or log files
- Conveniently reference specific line numbers when discussing file content
- Number file content for subsequent processing
- Generate documents or reports with line numbers
Althoughcat -ncan also achieve similar functionality, butnlprovides richer line number control options, which can meet more complex needs.
2. Basic Syntax and Parameters
2.1 Basic Command Format
Example
nl [Option]... [File]...
2.2 Description of Common Options
| Option | Description |
|---|---|
-b |
Specify the line number calculation rule (followed by a style) |
-i |
Set the line number increment (default is 1) |
-n |
Set the line number format (left-aligned/right-aligned/without zero padding) |
-s |
Set the separator between the line number and the content |
-v |
Set the starting line number (default is 1) |
-w |
Set the line number field width (number of characters) |
-p |
Do not reset line numbers at page breaks |
2.3 Line Number Calculation Rules (-b Option)
-bThe option can be followed by the following styles:
| Style | Description |
|---|---|
a |
Number all lines |
t |
Number only non-empty lines (default) |
n |
Do not number |
p正则表达式 |
Number only lines matching the regular expression |
3. Practical Example Demonstrations
3.1 Basic Usage: Adding Line Numbers to a File
Example
# Add line numbers to all non-empty lines of example.txt
nl example.txt
# Add line numbers to all lines (including empty lines)
nl -b a example.txt
nl example.txt
# Add line numbers to all lines (including empty lines)
nl -b a example.txt
3.2 Customizing Line Number Format
Example
# Set the line number width to 3, using right-aligned format
nl -n rz -w 3 example.txt
# Use "|" as the separator, line numbers left-aligned
nl -s " | " -n ln example.txt
nl -n rz -w 3 example.txt
# Use "|" as the separator, line numbers left-aligned
nl -s " | " -n ln example.txt
3.3 Advanced Numbering Rules
Example
# Start numbering from 10 with an increment of 2
nl -v 10 -i 2 example.txt
# Number only lines containing "error"
nl -b perror example.log
nl -v 10 -i 2 example.txt
# Number only lines containing "error"
nl -b perror example.log
3.4 Processing Multiple Files
Example
# Number multiple files consecutively
nl file1.txt file2.txt
# Number each file separately
nl -p file1.txt file2.txt
nl file1.txt file2.txt
# Number each file separately
nl -p file1.txt file2.txt
4. Comparison with Other Commands
4.1 nl vs cat -n
| Feature | nl | cat -n |
|---|---|---|
| Default behavior | Number only non-empty lines | Number all lines |
| Format control | Rich (alignment, width, separator, etc.) | Limited |
| Numbering rules | Customizable (regular expression matching, etc.) | Fixed |
| Performance | Slightly slower | Slightly faster |
4.2 nl vs grep -n
grep -nIt also displays line numbers, but its main purpose is search and filtering, and line numbers are just additional information.nlnl, on the other hand, is a tool specifically optimized for line number display.
5. Practical Application Scenarios
5.1 Code Review
Example
# Generate a code file with line numbers for easy discussion
nl -w 3 -n rz source.py > numbered_source.txt
nl -w 3 -n rz source.py > numbered_source.txt
5.2 Log Analysis
Example
# Number only log lines containing "ERROR"
nl -b pERROR app.log | less
nl -b pERROR app.log | less
5.3 Document Processing
Example
# Generate a Markdown document with line numbers
nl -s ". " -w 2 -n rz README.md
nl -s ". " -w 2 -n rz README.md
6. Notes and Tips
- Empty line handling: By default,
nlempty lines are skipped; use-b ato force numbering for all lines. - Large file handling: For very large files, consider combining
lessorheadto use - Pipeline usage: It can accept standard input, such as
cat file | nl - Format consistency: It is recommended to use a fixed set of parameters in scripts to ensure consistent output format.
- Performance considerations: For simple line numbering needs,
cat -nmay be faster.
7. Summary and Exercises
7.1 Exercise Problems
- Create a test file
test.txtContaining 5 lines of text and 2 empty lines - Use
nlto display all line numbers (including empty lines) - Set line numbers to start from 100 with an increment of 5
- Use "::" as the separator and a line number width of 4
- Redirect the result to
numbered.txt
7.2 Reference Answers
Example
# Create a test file
echo -e "First line\n\nSecond line\nThird line\n\nFourth line\nFifth line" > test.txt
# Execute the nl command
nl -b a -v 100 -i 5 -s "::" -w 4 test.txt > numbered.txt
# View the result
cat numbered.txt
echo -e "First line\n\nSecond line\nThird line\n\nFourth line\nFifth line" > test.txt
# Execute the nl command
nl -b a -v 100 -i 5 -s "::" -w 4 test.txt > numbered.txt
# View the result
cat numbered.txt
Linux Command Reference