Linux nl Command
## 1. nl Command Overview `nl` is a simple but practical command-line tool in Linux systems, full name "number lines". Its main function is to read file content and add line numbers to each line, then output the result to standard output. ### 1.1 Why We Need nl Command In daily development or system management, we often need to: * Quickly view specific lines of code or log files * Easily reference specific line numbers when discussing file content * Number file content for subsequent processing * Generate documents or reports with line numbers Although `cat -n` can achieve similar functionality, `nl` provides richer line number control options to meet more complex requirements.
## 2. Basic Syntax and Parameters ### 2.1 Basic Command Format
Example
nl ... ...
### 2.2 Common Options Description
| Option | Description |
| --- | --- |
| `-b` | Specify line number calculation rules (followed by style) |
| `-i` | Set line number increment (default is 1) |
| `-n` | Set line number format (left-aligned/right-aligned/no zero padding) |
| `-s` | Set separator between line number and content |
| `-v` | Set starting line number (default is 1) |
| `-w` | Set line number field width (number of characters) |
| `-p` | Do not reset line number at page breaks |
### 2.3 Line Number Calculation Rules (-b Option)
The following styles can be used after the `-b` option:
| Style | Description |
| --- | --- |
| `a` | Number all lines |
| `t` | Number non-empty lines only (default value) |
| `n` | No numbering |
| `pRE` | Only number lines matching the regular expression |
## 3. Practical Examples ### 3.1 Basic Usage: Adding Line Numbers to Files
Example
# Add line numbers to all non-empty lines in 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 line number width to 3, use right-aligned format
nl -n rz -w 3 example.txt
# Use "|" as separator, left-align line numbers
nl -s" | " -n ln example.txt
### 3.3 Advanced Numbering Rules
Example
# Start numbering from 10, increment by 2
nl -v 10 -i 2 example.txt
# Only number 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
## 4. Comparison with Other Commands ### 4.1 nl vs cat -n | Feature | nl | cat -n | | --- | --- | --- | | Default behavior | Number non-empty lines only | Number all lines | | Format control | Rich (alignment, width, separator, etc.) | Limited | | Numbering rules | Customizable (regex 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 filtering, and line numbers are just additional information. `nl` is a tool specifically optimized for line number display.
## 5. Practical Application Scenarios ### 5.1 Code Review
Example
# Generate a numbered code file for easy discussion
nl -w 3 -n rz source.py > numbered_source.txt
### 5.2 Log Analysis
Example
# Only number log lines containing "ERROR"
nl -b pERROR app.log | less
### 5.3 Document Processing
Example
# Generate a numbered Markdown document
nl -s". " -w 2 -n rz README.md
## 6. Notes and Tips 1. **Empty Line Handling**: By default, `nl` skips empty lines. Use `-b a` to force numbering all lines 2. **Large File Processing**: For very large files, consider using with `less` or `head` 3. **Pipe Usage**: Can receive standard input, e.g., `cat file | nl` 4. **Format Consistency**: It is recommended to fix a set of parameters in scripts to ensure uniform output format 5. **Performance Consideration**: For simple line number needs, `cat -n` may be faster
## 7. Summary Exercise ### 7.1 Exercise Questions 1. Create a test file `test.txt` containing 5 lines of text and 2 empty lines 2. Use `nl` to display all line numbers (including empty lines) 3. Set the line number to start from 100, with an increment of 5 4. Use "::" as the separator, with line number width of 4 5. Redirect the result to `numbered.txt` ### 7.2 Reference Answers
Example
# Create test file
echo -e "First linennSecond linenThird linennFourth linenFifth line" > test.txt
YouTip