Linux nl command: displays the line number of the file and the contents of the file
Linux nl command Function Description
Use the nl command to calculate the line numbers in the file.
nl can automatically add line numbers to the output file content, and its default result is not quite the same as the cat -n command.
nl can display the line numbers in more design, including the number of bits and whether to automatically complete the 0 and other functions.
Linux nl command Syntax
nl [Option] [File]
The meaning of each option in the command is shown in the table:
Option | Description |
---|---|
-b <Style> |
The following four styles are available for numbering the body lines of a document using the specified style: a: Numbering of all rows t: Numbering of non-blank lines n:No line numbering pBRE: Only number the lines that match the regular expression BRE |
-f <Style> |
Use the specified style to number the footer lines of the document |
-h <Style> |
Number the header lines of the document using the specified style |
-i <Numerical value> |
Set the auto-increment value after each line traversal |
-l <Numerical value> |
A number of blank lines with a set value are treated as one line |
-p | Does not reset the line number value when logical page number switching |
-v <Digital> |
Line number of the first line on each logical page |
-s <String> |
Add the string after the line number if possible |
-n <Format> |
Line numbers are inserted according to the specified format in 3 main ways: ln: Left-aligned, no 0 padding for spaces rn: Right-justified, no 0 padding for spaces rz: Right-justified, spaces filled with 0 |
Linux nl command Demo
Show the contents of the /root/gg file
[root@rhel ~]# nl /root/gg
1 Hello Linux
2 Hello Red Hat
// For blank lines in a file, the nl command does not add line numbers by default
Show the contents of the /root/gg file, with the line number added to blank lines
[root@rhel ~]# nl -b a /root/gg
1 Hello Linux
2
3 Hello Red Hat
Display the contents of the /root/gg file so that the line numbers are automatically preceded by zeros and the output format is unified
[root@rhel ~]# nl -b a -n rz /root/gg
000001 Hello Linux
000002
000003 Hello Red Hat
// Line number is 6 digits
Display the contents of the /root/gg file, so that the line number is automatically preceded by a 0, and the line number is 3 digits
[root@rhel ~]# nl -b a -n rz -w 3 /root/gg
001 Hello Linux
002
003 Hello Red Hat
// Line number is now 3 digits