Skip to content

nl MacOS command

The nl command in MacOS is used to add line numbers to files. This can be helpful when working with large amounts of text and you need to keep track of specific lines. The nl command offers various options to customize the output, such as adding a header, specifying the line number format, and more.

To use the nl command, simply open a terminal window and type nl followed by the file you want to add line numbers to. You can also use options like -b to specify how to number non-empty lines, -w to set the line number width, and -s to define the line number separator.

Overall, the nl command is a useful tool for organizing and annotating text files in MacOS. By familiarizing yourself with its options and functionality, you can streamline your text processing tasks and improve your workflow efficiency.

nl Syntax:

Terminal window
nl [option] [file]

Options:

OptionDescription
-bSpecify the line numbering style
-fSet the number increment for line numbers
-hDisplay help information
-iSet the line number increment
-lSpecify the line numbering style
-nSpecify the line numbering style
-pDon’t reset line numbers at logical pages
-sSet the number separator for line numbers
-vIncrease the line number size
-wSet the line number width
-zDisplay leading zeros in the line numbers

Parameters:

ParameterDescription
fileThe file whose lines will be numbered

nl bash Examples:

Number Lines in a File

Terminal window
nl file.txt

Adds line numbers to the content of the file “file.txt”.

Display Line Numbers at the End of Lines

Terminal window
nl -ba file.txt

Displays line numbers at the end of each line in the file “file.txt”.

Customize the Line Numbering Style

Terminal window
nl -n rz -w 5 file.txt

Customizes the line numbering style in the file “file.txt” to use a leading zero with a width of 5 characters.

Display Empty Line Numbers

Terminal window
nl -ba -s'' file.txt

Displays line numbers for empty lines using an empty string separator in the file “file.txt”.

Omit Line Numbering on Specific Lines

Terminal window
nl -v 100 file.txt

Omits line numbering for lines starting at 100 in the file “file.txt”.

Number Only Non-empty Lines

Terminal window
nl -b a file.txt

Numbers only non-empty lines in the file “file.txt”.

How do I use nl in MacOS?

To use the nl command in MacOS, execute the following command:

Terminal window
nl <file_name>

What is the purpose of the nl command in MacOS?

The nl command in MacOS is used to number lines in a file before displaying them on the standard output.

How can I display line numbers in nl output?

To display line numbers in the nl output, use the -n option followed by the numbering style. For example:

Terminal window
nl -n ln <file_name>

How can I add a delimiter between line numbers and the content?

To add a delimiter between line numbers and the content, use the -s option followed by the delimiter. For example:

Terminal window
nl -s '. ' <file_name>

How can I customize the line numbering format in nl?

To customize the line numbering format in nl, use the -b option followed by the type of numbering. For example:

Terminal window
nl -b a <file_name>

How can I specify the increment for line number in nl output?

To specify the increment for line number in nl output, use the -i option followed by the desired increment value. For example:

Terminal window
nl -i 5 <file_name>

How can I number non-empty lines only in nl?

To number non-empty lines only in nl, use the -ba option. For example:

Terminal window
nl -ba <file_name>

How can I suppress line numbers for certain lines in nl output?

To suppress line numbers for certain lines in nl output, use the -v option followed by the lines to ignore. For example:

Terminal window
nl -v 2,5,10 <file_name>

Applications of the nl command

  • Numbering lines in a file
  • Adding line numbers to the output
  • Formatting text for readability
  • Creating ordered lists from text data
  • Identifying and referencing specific lines in a file