Introduction to the use of nl command in Linux training tutorial


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, etc. Function. Brother even linux training

Xiaobian to introduce: the introduction of the

nl
command in linux.

  1. Command format:

  nl [options]... [files]...

  2. Command parameters:

  -b : There are two ways to specify the line number:

  -ba : No matter whether it is a blank line or not, the same is true List line numbers (similar to cat -n);

  -bt : If there is an empty line, do not list the line number on the empty line (default value);

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

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

  -n rn : the line number is displayed on the far right of its own field without adding 0;

  -n rz : the line number is displayed on the far right of its own field, and Add 0 ;

  -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

  Command :

  nl log2012.log

  Output:

  Copy the code as follows:

  [root@localhost test]# nllog2012.log

  1

  2012-01 2 2012-02

  3 = =====[root@localhosttest]#

  Description: For

  blank lines in the file, nl will not add line numbers

  Example 2: Use nl to list the contents of log2012.log, and add line numbers to empty lines

  Command:

  nl -ba log2012.log

  output:

  copy the code The code is as follows:

  [root@localhost test]# nl-b a log2012.log

  1

  2012-01 2 2012-02

  3

  4

  5 ======[root@localhosttest]#Example

  3: Automatically add 0 to the front of the line number, and unify the output format

  Command :

  Output:

  Copy the code The code is as follows:

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

  Explanation:

  nl -ba -n rz command line The default number is six digits. To adjust the number of digits, you can add the parameter -w 3 to adjust it to 3 digits.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326489128&siteId=291194637