Common Linux commands (10) and common Linux commands (
The nl command is used in linux to calculate the row number in the file. Nl can automatically add the row number to the output file content! The default result is a little different from cat-n. nl can compare multiple display designs of the row number, including the number of digits and whether to automatically complete 0.
1.Command Format:
Nl [Option]... [file]...
2.Command parameters:
-B: Specifies the row number in two ways:
-B a: indicates that the row number (similar to cat-n) is also listed no matter whether the row is empty or not );
-B t: if there is a blank row, do not list the row number (default) for the empty row );
-N: Used to list row numbers. There are three methods:
-N ln: the row number is displayed on the leftmost side of the screen;
-N rn: the row number is displayed on the rightmost side of its own column without 0;
-N rz: the row number is displayed on the rightmost side of its own column with a value of 0;
-W: The number of digits occupied by the row number column.
-P does not start computing again at the logical delimiters.
3.Command function:
The nl command reads the File parameter (standard input by default), calculates the row number in the input, and writes the computed row number to the standard output. In the output, the nl command calculates the left line based on the flag specified in the command line. The input text must be written on the logic page. Each logic page has a header, body, and footer section (you can leave a blank section ). Unless the-p Flag is used, the nl command resets the row number at the beginning of each logic page. You can set a line calculation flag for the header, body, and footer sections separately (for example, the header and footer rows can be calculated, but the text rows cannot ).
4.Instance used:
Instance 1:Use nl to list log2012.log content
Command: nl log2012.log
[root@localhost test]# nl log2012.log 1 2012-01 2 2012-02 3 ======[root@localhost test]#
Example 2:Use nl to list the content of log2012.log, and add a line number to empty rows.
Command: nl-B a log2012.log
[root@localhost test]# nl -b a log2012.log 1 2012-01 2 2012-02 3 4 5 ======[root@localhost test]#
Example 3:Set 0 in front of the line number to unify the output format.
Command: nl-B a-n rz log2014.log
Note: The default line number of the nl-B a-n rz command is six digits. To adjust the number of digits, you can add the parameter-w 3 to three digits.
[root@localhost test]# nl -b a -n rz log2014.log 000001 2014-01000002 2014-02000003 2014-03000004 =======[root@localhost test]# nl -b a -n rz -w 3 log2014.log 001 2014-01002 2014-02003 2014-03004 =======