Linux nl command
I. Overview of the nl command
nl is a simple but useful command-line tool in Linux, short for "number lines". Its main function is to read file contents, add a line number to each line, and output the result to standard output.
1.1 Why the nl command is needed
In daily development or system administration, we often need to:
- Quickly view specific lines of a code or log file
- Conveniently reference specific line numbers when discussing file contents
- Number the lines of a file for later processing
- Generate documents or reports with line numbers
Although cat -n can also achieve similar functionality, but nl provides richer line-number control options that can meet more complex needs.
II. Basic syntax and parameters
2.1 Basic command format
Example
nl
[
Option
]
...
[
file
]
...
2.2 Common options
| Option | Description |
|---|---|
-b |
Specify the line numbering rule (followed by a style) |
-i |
Set the line number increment (default is 1) |
-n |
Set the line number format (left-aligned/right-aligned/no 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 width of the line number field (number of characters) |
-p |
Do not reset line numbers at page breaks |
2.3 Line numbering rules (-b option)
-b The following styles can follow the option:
| Style | Description |
|---|---|
a |
Number all lines |
t |
Number only non-empty lines (default) |
n |
No numbering |
p正则表达式 |
Number only lines matching a regular expression |
III. Practical examples
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 ones)
nl
-b
a example.txt
3.2 Customizing the 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, with line numbers left-aligned
nl
-s
" | "
-n
ln
example.txt
3.3 Advanced numbering rules
Example
Start numbering at 10 with an increment of 2
nl
-v
10
-i
2
example.txt
Number only lines containing "error"
nl
-b
perror
example.log
3.4 Handling multiple files
Example
Number multiple files continuously
nl
file1.txt file2.txt
Number each file separately
nl
-p
file1.txt file2.txt
IV. 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, separators, etc.) | Limited |
| Numbering rules | Customizable (regular expression matching, etc.) | Fixed |
| Performance | Slightly slower | Slightly faster |
4.2 nl vs grep -n
grep -n also displays line numbers, but its main purpose is search and filtering; line numbers are only additional information.nl is a tool specifically optimized for displaying line numbers.
V. Practical application scenarios
5.1 Code review
Example
Generate a code file with line numbers for easier discussion
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
5.3 Document processing
Example
Generate Markdown documents with line numbers
nl
-s
". "
-w
2
-n
rz README.md
VI. Notes and tips
- Handling empty lines: By default
nlskips empty lines; use-b ato force numbering of all lines - Large file processing: For very large files, consider combining with
lessorheadUse - a pipe.: It can accept standard input, such as
cat file | nl - Format consistency: It is recommended to use a fixed set of options in scripts to ensure a consistent output format
- Performance considerations: For simple line-numbering needs,
cat -nmay be faster
VII. Summary and exercises
7.1 Exercises
- Create a test file
test.txtcontaining 5 lines of text and 2 empty lines - Use
nlDisplay all line numbers (including empty lines) - Set numbering to start at 100 with an increment of 5
- Use "::" as the separator with a line number width of 4
- Redirect the result to
numbered.txt
7.2 Reference answers
Example
Create the 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
Linux Command Reference