Linux nl command - calculate the line numbers in the file

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
Like(0)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?