The nl command is used in the Linux system to calculate the line number in the file. nl can automatically add line numbers to the output file content! The default result is a bit different from cat -n. nl can display more line numbers, including the number of digits and whether to automatically fill in 0 and other functions.
1. Command format:
nl [options]... [files]...
2. Command parameters:
-b : There are two main ways to specify the line number:
-b a : Indicates that the line number is also listed regardless of whether it is an empty line (similar to cat -n);
-b t : If there is an empty line, do not list the line number on the empty line (default value);
-n : List the methods represented by line numbers, there are three main types:
-n ln : The line number is displayed at the far left of the screen;
-n rn : the line number is displayed at the far right of its own field without adding 0;
-n rz : The line number is displayed at the far right of its own field, and 0 is added;
-w : The number of bits occupied by the line number field.
-p Do not restart computation at logical delimiters.
3. Command function:
The nl command reads the File parameter (standard input by default), calculates the line number in the input, and writes the calculated line number to standard output. In the output, the nl command evaluates the lines to the left based on the flags you specify on the command line. Input text must be written in logical pages. Each logical page has header, body and footer sections (may have empty sections). Unless the -p flag is used, the nl command resets line numbers at the beginning of each logical page. Line count flags can be set separately for header, body, and footer sections (eg, header and footer lines can be counted but text lines cannot).
4. Example of use:
Example 1: List the contents of log2012.log with nl
Order:
nl log2012.log
output:
[root@localhost test]# nl log2012.log 1 2012-01 2 2012-02 3 ======[root@localhost test]#
illustrate:
Blank lines in file, nl will not add line numbers
Example 2: Use nl to list the contents of log2012.log, and add the line number to the empty line
Order:
nl -b a log2012.log
output:
[root@localhost test]# nl -b a log2012.log 1 2012-01 2 2012-02 3 4 5 ======[root@localhost test]#
Example 3: Automatically add 0 to the front of the line number to unify the output format
Command: nl -ba -n rz log2014.log
output:
[root@localhost test]# nl -b a -n rz log2014.log 000001 2014-01 000002 2014-02 000003 2014-03 000004 2014-04 000005 2014-05 000006 2014-06 000007 2014-07 000008 2014-08 000009 2014-09 000010 2014-10 000011 2014-11 000012 2014-12 000013 ======= [root@localhost test]# nl -b a -n rz -w 3 log2014.log 001 2014-01 002 2014-02 003 2014-03 004 2014-04 005 2014-05 006 2014-06 007 2014-07 008 2014-08 009 2014-09 010 2014-10 011 2014-11 012 2014-12 013 =======
illustrate:
nl -b a -n rz The command line number defaults to six digits. To adjust the number of digits, you can add the parameter -w 3 to adjust it to 3 digits.