How to Use the Command 'nl' (with Examples)

How to Use the Command 'nl' (with Examples)

The nl command is a Unix utility used to number the lines of a file or standard input (stdin). It is most commonly used to add line numbers to text files, which can be helpful for various text processing or documentation tasks. The command offers a flexible suite of options for customizing how lines are numbered, ranging from excluding blank lines to formatting line numbers in specific ways.

Use Case 1: Number Non-Blank Lines in a File

Code:

nl path/to/file

Motivation:

In situations where you need a clear reference for non-blank lines in a text file—such as code files or configuration scripts—numbering these lines can help you easily pinpoint specific lines for editing or debugging purposes.

Explanation:

  • nl: This is the base command to invoke the line-numbering function.
  • path/to/file: Replace this placeholder with the actual path and filename of the text file you want to number. This tells nl where to get the text input.

Example Output:

     1	This is a line of text.
     2	Here is another line.
     3	And yet another.

Use Case 2: Read from stdin

Code:

command | nl -

Motivation:

Sometimes you might want to insert line numbers into the output of a command directly, without first saving it to a file. This can be useful in pipelines in shell scripting where you want to monitor or report line numbers of a generated output in real time.

Explanation:

  • command: Substitute command with any shell command whose output you want to number.
  • |: The pipe symbol is used to pass the output of command as input to nl.
  • nl: Invokes the nl line numbering function.
  • -: Uses the dash symbol to signify input from stdin.

Example Output:

     1	Output line 1 from the command
     2	Output line 2 from the command

Use Case 3: Number All Body Lines Including Blank Lines or Do Not Number Body Lines

Code:

nl -b a|n path/to/file

Motivation:

Depending on the context, numbering each line—even blank ones—may be necessary for clarity, especially in educational materials or documentation. Alternatively, skipping numbers for body lines can be used when formatting strictly requires no interruptions for certain blocks of text.

Explanation:

  • nl: Invokes the line numbering function.
  • -b a|n: -b specifies the numbering format for body lines. a is for numbering all lines, including blanks. n for not numbering any body lines.
  • path/to/file: This indicates the file being processed.

Example Output:

For -b a:

     1	This text has blank lines.
     2	
     3	All lines are numbered.

For -b n:

     	This text has no numbers on body lines.
     	
     	Even non-blank lines are not numbered.

Use Case 4: Number Only the Body Lines That Match a Basic Regular Expression (BRE) Pattern

Code:

nl -b p'FooBar[0-9]' path/to/file

Motivation:

You might need to selectively number lines that match a specific pattern, such as lines containing particular keywords or formats, which is useful in text analysis or data parsing operations.

Explanation:

  • nl: Initiates the function.
  • -b p'FooBar[0-9]': -b is for body numbering mode. p option to number lines matching a pattern. 'FooBar[0-9]' is a basic regular expression matching lines containing “FooBar” followed by a digit.
  • path/to/file: The file to operate on.

Example Output:

     	No numbers here.
     1	FooBar1
     	Just another line.
     2	FooBar2

Use Case 5: Use a Specific Increment for Line Numbering

Code:

nl -i increment path/to/file

Motivation:

Numbering with custom increments is useful when creating sections (e.g., step-by-step guides) where you need the lines differentiated by factors other than a simple consecutive sequence.

Explanation:

  • nl: Calls the line numbering utility.
  • -i increment: Sets the line numbering increment. Replace increment with your desired step value, e.g., 5.
  • path/to/file: The file input.

Example Output:

     5	First section.
    10	Second section.

Use Case 6: Specify the Line Numbering Format to Right or Left Justified, Keeping Leading Zeros or Not

Code:

nl -n rz|ln|rn path/to/file

Motivation:

Formatting line numbers for readability or alignment is important for polished documentation or code comments. You can right-justify or left-justify the numbers, with or without leading zeros, based on the context or readability needs.

Explanation:

  • nl: The command to start line numbering.
  • -n rz|ln|rn: Defines number alignment and format. rz for right-justified with zeros, ln for left-justified without zeros, rn for right-justified without zeros.
  • path/to/file: File path to process.

Example Output:

For -n rz:

00001	This is right-justified.
00002	With leading zeros.

For -n ln:

1	This is left-justified.
2	No leading zeros.

Use Case 7: Specify the Line Numbering’s Width

Code:

nl -w col_width path/to/file

Motivation:

Managing how many columns are dedicated for line numbers is necessary when aligning text visually or fitting it into specific document layouts. This ensures consistent spacing across documents.

Explanation:

  • nl: Initiates the line-numbering process.
  • -w col_width: Sets the width of the line numbers. Replace col_width with the desired width, e.g., 8.
  • path/to/file: Specifies the file to be processed.

Example Output:

      1	The width is eight columns.
      2	So there is extra space.

Use Case 8: Use a Specific String to Separate the Line Numbers from the Lines

Code:

nl -s separator path/to/file

Motivation:

Sometimes, default tab spacing might not suit your formatting needs—such as when preparing visually distinct outputs or converting files for another application that requires specific delimiters.

Explanation:

  • nl: Utilizes the line numbering function.
  • -s separator: Sets the string to separate numbers from text, e.g., ':'.
  • path/to/file: The target file path.

Example Output:

1:This uses a different separator.
2:The colon in this case.

Conclusion

The nl command offers flexibility and control over numbering lines in files or standard input, which can be particularly beneficial for text formatting, code editing, and documentation tasks. Understanding and utilizing its options allow for tailor-made formatting to meet a wide range of needs, from simple number addition to complex pattern matching and formatting.

Related Posts

How to use the command 'gitk' (with examples)

How to use the command 'gitk' (with examples)

Gitk is a graphical tool for browsing Git repositories, providing a user-friendly interface to navigate through the repository history.

Read More
How to use the command 'npm logout' (with examples)

How to use the command 'npm logout' (with examples)

The npm logout command is a utility provided by Node Package Manager (NPM) to log out a user from their NPM registry account.

Read More
Managing Node.js Versions with nodenv (with examples)

Managing Node.js Versions with nodenv (with examples)

Nodenv is a powerful tool that allows developers to manage different versions of Node.

Read More