The nl Command in Linux: Adding Line Numbers to Text Files

Linux provides various commands for text formatting and editing, and one particularly useful command is the nl command. This tool allows you to add line numbers to each line of a text file or input, making it easier to read and organize content.


What Does the nl Command Do?

The nl command displays the contents of a text file with line numbers added to non-empty lines by default. It’s especially useful when working with scripts or reviewing large files where tracking line numbers is critical for debugging or editing.


Example: Using the nl Command

Suppose you have a file named hello-world.sh containing the following script:

#!/bin/bash
#
#This is a demo script that greets the world
#Author: Jose Navarro Varela
#Usage: ./hello-world.sh

clear
echo hello world
exit 0

To display the file with line numbers, simply run:

nl hello-world.sh

The output will look like this:

     1 #!/bin/bash
     2 #
     3 #This is a demo script that greets the world
     4 #Author: Jose Navarro Varela
     5 #Usage: ./hello-world.sh
     6 clear
     7 echo hello world
     8 exit 0

By default, the nl command numbers only non-empty lines.


Numbering All Lines (Including Empty Lines)

To include empty lines in the numbering, use the -b a option:

nl -b a hello-world.sh

The output will look like this:

     1 #!/bin/bash
     2 #
     3 #This is a demo script that greets the world
     4 #Author: Jose Navarro Varela
     5 #Usage: ./hello-world.sh
     6
     7 clear
     8 echo hello world
     9 exit 0

Adding a Custom Separator

The nl command allows you to customize the separator between the line numbers and the text using the -s option. For example, to add ... as the separator:

nl -s "..." hello-world.sh

The output will look like this:

1...#!/bin/bash
2...#
3...#This is a demo script that greets the world
4...#Author: Jose Navarro Varela
5...#Usage: ./hello-world.sh
6...
7...clear
8...echo hello world
9...exit 0

Additional Options

The nl command offers more options for customization, such as:

  • -w: Specify the width of the line number field.
  • -n: Define the numbering format (right-aligned, left-aligned, or zero-padded).

You can explore all available options by checking the manual page with:

man nl

Common Scenarios for Using the nl Command

The nl command is widely used in various situations. Below are some of the most common scenarios:

1. Debugging Shell Scripts

When debugging a shell script, you may need to refer to specific lines where errors occur. Using the nl command to add line numbers makes it easier to pinpoint the problematic lines.

Example:

nl my-script.sh

2. Preparing Documentation or Code Reviews

When sharing scripts or code snippets for review, adding line numbers makes the content more readable and easier to reference during discussions.

Example:

nl config.conf > numbered-config.conf

3. Reviewing Large Text Files

When working with large log files, configuration files, or data files, adding line numbers helps you navigate and reference specific sections.

Example:

nl /var/log/syslog

4. Comparing Files with Line Numbers

Adding line numbers to files before comparing them makes it easier to identify differences.

Example:

nl file1.txt > numbered-file1.txt
nl file2.txt > numbered-file2.txt
diff numbered-file1.txt numbered-file2.txt

5. Formatting Output for Reports

The nl command can be used to format text files for reports or presentations where numbered lines are required for clarity.

Example:

nl report.txt > numbered-report.txt

6. Viewing Scripts with Empty Line Numbers

Sometimes scripts contain empty lines that are significant for readability. You can use the -b a option to number all lines, including empty ones.

Example:

nl -b a script.sh

7. Customizing Line Numbers for Specific Formats

When presenting numbered text files, you might need custom separators or formatting.

Example:

nl -s " -> " textfile.txt

Why Use the nl Command?

The nl command is a simple yet powerful tool for:

  • Debugging scripts by identifying specific lines.
  • Adding line numbers to text files for easier navigation.
  • Preparing formatted output for documentation or sharing.

Leave a Comment