Practical Applications and Advanced Usage
In this final step, we'll explore some practical applications and advanced usage of the nl command in real-world scenarios.
Numbering Multiple Files
The nl command can process multiple files at once. Let's create a second file:
nano commands2.txt
Add the following content:
Additional Linux Commands
------------------------
find - search for files
tar - archive files
ssh - secure shell connection
df - disk free space
top - display system processes
Save and exit nano. Now let's number both files together:
nl sample.txt commands2.txt > combined_numbered.txt
View the combined result:
cat combined_numbered.txt
You should see both files numbered sequentially:
1 Linux Commands
2 -------------
3 cat - display file contents
4 ls - list directory contents
5 cd - change directory
6 grep - search for patterns
7 chmod - change file permissions
1 Additional Linux Commands
2 ------------------------
3 find - search for files
4 tar - archive files
5 ssh - secure shell connection
6 df - disk free space
7 top - display system processes
Notice that the line numbers restart for the second file. If you want continuous numbering across files, you can use the -i option:
nl -i 1 -n ln sample.txt commands2.txt > continuously_numbered.txt
View the result:
cat continuously_numbered.txt
You should see continuous numbering across both files:
1 Linux Commands
2 -------------
3 cat - display file contents
4 ls - list directory contents
5 cd - change directory
6 grep - search for patterns
7 chmod - change file permissions
8 Additional Linux Commands
9 ------------------------
10 find - search for files
11 tar - archive files
12 ssh - secure shell connection
13 df - disk free space
14 top - display system processes
Combining with Other Commands
The nl command can be combined with other Linux commands using pipes. For example, you can number the lines of command output:
ls -l /etc | nl > numbered_ls_output.txt
View the result:
cat numbered_ls_output.txt
You should see the output of ls -l /etc with added line numbers.
Real-World Use Case: Adding Line Numbers to a Log File
Line numbering is especially useful when analyzing log files. Let's see how we can use nl to add line numbers to logs:
## First, create a sample log file
cat > sample_log.txt << EOF
[2023-07-01 10:15:22] INFO: System startup
[2023-07-01 10:15:24] INFO: Loading configuration
[2023-07-01 10:15:25] WARNING: Config file is outdated
[2023-07-01 10:15:28] ERROR: Failed to connect to database
[2023-07-01 10:15:30] INFO: Retrying database connection
[2023-07-01 10:15:33] INFO: Database connection established
[2023-07-01 10:15:35] INFO: System ready
EOF
Now, add line numbers with a custom format that includes the line number in brackets:
nl -s ' [Line: ' -n ln -w 2 -b a sample_log.txt | sed 's/$/]/' > numbered_log.txt
This command:
- Uses a custom separator
-s ' [Line: '
- Uses left-justified numbers with no leading zeros
-n ln
- Sets the width to 2 characters
-w 2
- Numbers all lines
-b a
- Uses
sed to add a closing bracket at the end of each line
View the result:
cat numbered_log.txt
You should see log entries with line numbers in brackets:
1 [Line: [2023-07-01 10:15:22] INFO: System startup]
2 [Line: [2023-07-01 10:15:24] INFO: Loading configuration]
3 [Line: [2023-07-01 10:15:25] WARNING: Config file is outdated]
4 [Line: [2023-07-01 10:15:28] ERROR: Failed to connect to database]
5 [Line: [2023-07-01 10:15:30] INFO: Retrying database connection]
6 [Line: [2023-07-01 10:15:33] INFO: Database connection established]
7 [Line: [2023-07-01 10:15:35] INFO: System ready]
This format can be very useful when referencing specific log entries in documentation or discussions.
Now you have a good understanding of how to use the nl command for basic and advanced line numbering in Linux. This skill will be valuable when working with text files, logs, code, and documentation.