nl
The command in Linux, which is used for numbering lines in text files. We’ll learn how to use this command in various scenarios, making it easier to reference specific lines in documents. This skill is particularly useful for programmers, system administrators, and anyone who works with text files regularly.
nl file_name
- The nl command has added line numbers to the left of each line in the file.
- Notice that the numbers are right-aligned in a 6-character wide column.
- The blank line (line 4 in the original file) is not numbered by default.
Numbering All Lines, Including Blank Lines
Sometimes you might want to number all lines, including blank ones. This can be useful when you need to reference empty lines or when working with files where blank lines are significant. Let’s use the -b a option to achieve this.
Run the following command:
nl -b a file_name
- The -b option controls the line numbering for the body of the file.
- The a argument stands for “all” and tells nl to number all lines, including blank ones.
- Notice that line 4, which was blank and unnumbered before, now has a number.
Customizing Number Format
The nl command allows you to customize how line numbers are displayed. This can be useful for improving readability or for preparing output for further processing. Let’s try right-aligning the numbers and adding leading zeros.
Use the -n rz option:
nl -n rz file_name
Output should look like:
000001 # Server Configuration
000002 port=8080
000003 max_connections=100
000004 # Database Settings
000005 db_host=localhost
000006 db_port=5432
000007 db_name=myapp
000008 ## Logging Configuration
000009 log_level=info
000010 log_file=/var/log/myapp.log
000011 # Security Settings
000012 enable_ssl=true
000013 ssl_cert_path=/etc/ssl/certs/myapp.crt
000014 ## Performance Tuning
000015 cache_size=1024
000016 thread_pool=20
000017 # Miscellaneous
000018 debug_mode=false
- -n is used to specify the numbering format.
- r means right-aligned (which is actually the default).
- z means adding leading zeros.
Numbering Specific Line Types
The nl command allows you to number only specific types of lines. This can be extremely useful when working with complex files where you want to focus on certain lines. Let’s number only the non-empty lines that don’t start with a ‘#’ character (which are often used for comments in configuration files).
Use the following command:
nl -b p'^[^#]' file_name
you should see output like:
# Server Configuration
1 port=8080
2 max_connections=100
# Database Settings
3 db_host=localhost
4 db_port=5432
5 db_name=myapp
## Logging Configuration
6 log_level=info
7 log_file=/var/log/myapp.log
# Security Settings
8 enable_ssl=true
9 ssl_cert_path=/etc/ssl/certs/myapp.crt
## Performance Tuning
10 cache_size=1024
11 thread_pool=20
# Miscellaneous
12 debug_mode=false
- -b p tells nl to number only the lines that match a specific pattern.
- ‘^[^#]’ is a regular expression pattern:
- ^ means “start of the line”
- [^#] means “any character that is not #”
- So together, this matches any line that doesn’t start with #
Combining Options for Complex Numbering
Now that we’ve explored several options of the nl command, let’s combine them to create a more complex numbering scheme. We’ll number all lines, use a custom format, and restart numbering for each section.
Run the following command:
nl -b a -n rz -s ': ' -w 3 file_name
This command does the following:
-b a: Number all lines, including blank ones
-n rz: Right-align numbers with leading zeros
-s ': ': Use ': ' as the separator between the number and the line content
-w 3: Set the width of the number field to 3 characters
You should see something like:
001: # Server Configuration
002: port=8080
003: max_connections=100
004:
005: # Database Settings
006: db_host=localhost
007: db_port=5432
008: db_name=myapp
009:
010: ## Logging Configuration
011: log_level=info
012: log_file=/var/log/myapp.log
013:
014: # Security Settings
015: enable_ssl=true
016: ssl_cert_path=/etc/ssl/certs/myapp.crt
017:
018: ## Performance Tuning
019: cache_size=1024
020: thread_pool=20
021:
022: # Miscellaneous
023: debug_mode=false
- All lines are numbered, including blank lines.
- Numbers are right-aligned with leading zeros.
- The separator between the number and the line content is ‘: ‘.
- The width of the number field is set to 3 characters.
Summary
In this tutorial, we explored the nl command and its various options for numbering lines in text files. We learned how to:
Use the basic nl command to add line numbers to a file
Number all lines, including blank ones, using the -b a option
Customize the number format with the -n rz option
Number specific line types using pattern matching
Combine multiple options for complex numbering schemes
These skills will help you navigate and reference lines in configuration files, source code, or any text files you work with in your development projects.
Additional nl command options not covered in this tutorial include:
-v NUM: Start numbering at NUM instead of 1
-i NUM: Increment numbers by NUM instead of 1
-l NUM: Group NUM lines together and only number the first line of each group
-f a: Number all header lines (lines before the first body line)
These options provide even more flexibility when working with the nl command.