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 config.txt
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 output similar to this:
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
Let's break down what's happening:
- 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.
This complex numbering scheme can be very useful when working with structured configuration files or documents with distinct sections. It allows you to reference lines within sections easily.
If your output doesn't match this exactly, carefully review the command you entered, paying special attention to the spaces and special characters.