Detailed usage of nl commands under Linux

  
                

The nl command in Linux is similar to the cat command, but the nl command will play the uplink number, which is a less common command. Let's take a look at this little-known nl command with Xiaobian.

nl command to calculate the number of rows in the linux file system. Nl can automatically add the line number to the output file content! The default result is a bit different from cat -n. nl can do more display design for line numbers, including the number of bits and whether to automatically fill 0 and so on.

1. Command format:

nl [options]. . . [file]. . .

2. Command parameters:

-b : Specify the way the line number is specified. There are two main ways:

-ba : Indicates whether the line number is the same whether it is a blank line or not (like cat - n);

-bt : If there is a blank line, the empty line should not list the line number (default value);

-n : List the method of line number representation, there are three Species:

-n ln : The line number is displayed on the far left of the screen;

-n rn : The line number is displayed at the far right of its own field, without adding 0; Br>

-n rz : The line number is displayed at the far right of its field and is incremented by 0;

-w : The number of bits occupied by the line number field.

-p Does not restart the calculation at the logical delimiter.

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 the standard output. In the output, the nl command calculates the left line based on the flags you specify on the command line. The input text must be written in a logical page. Each logical page has a header, body, and footer section (you can have an empty section). Unless the -p flag is used, the nl command resets the line number at the beginning of each logical page. Line calculation flags can be set separately for the header, body, and footer sections (for example, header and footer lines can be calculated but text lines cannot.).

4. Example:

Instance 1: List the contents of log2012.log with nl

Command:

nl log2012.log

Output:

[root@localhost test]# nl log2012.log

1 2012-01

2 2012-02

3 ======[root@ Localhost test]#

Description:

Blank lines in the file, nl will not add the line number

Example 2: List the contents of log2012.log with nl, empty The line is also added to the line number

Command:

nl -ba log2012.log

Output:

[root@localhost test]# nl -ba Log2012.log

1 2012-01

2 2012-02

3

4

5 ====== [root@localhost test]#

Instance 3: Let the line number be automatically padded with 0, unified output format

Command:

Output:

[root@localhost test]# nl -ba -n rz log2014.log

000001 2014-01

000002 2014-02

000003 2014-0 3

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 -ba -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 =======

Description:

nl -ba -n rz The command line number defaults to six digits. -w 3 is adjusted to 3 digits.

The above is the introduction of the nl command under Linux. This command can calculate the number of lines of the file, because it is rarely used and rarely seen. You can try it out.

Copyright © Windows knowledge All Rights Reserved