A Linux command a day (11): nl command

post thumb
Devops
by Admin/ on 23 Sep 2021

A Linux command a day (11): nl command


The nl command is used on linux systems to calculate line numbers in files. nl can automatically add line numbers to the output file! The default result is not quite the same as cat -n, but nl can display the line number in more design, including the number of digits and whether to auto-complete the 0, etc.

1. Command format.

nl \ [option ]… [file]…

2. Command parameters

-b :Specify the way of line number designation, there are two main types.

-b a : indicates that the line number is also listed regardless of whether there are blank lines (similar to cat -n).

-b t : if there are empty lines, do not list the line number on the empty line (default value).

-n : the method of listing the line number representation, there are three main types.

-n ln : the line number is displayed on the leftmost side of the screen

-n rn : the line number is displayed on the rightmost side of its own column, without adding 0; -n rz : the line number is displayed on the leftmost side of the screen

-n rz : the line number is displayed at the far right of its own column, and 0 is added; -n rz : the line number is displayed at the far right of its own column, and 0 is added

-w : the number of occupied bits in 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 according to the flags you specify in the command line. The input text must be written in a logical page. Each logical page has a header, body, and footer section (there can be empty sections). Unless the -p flag is used, the nl command resets the line number at the beginning of each logical page. Line count flags can be set separately for header, body, and footer sections (for example, header and footer lines can be counted but not text lines).

4. Examples of use.

Example 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 line numbers

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

Commands:

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: Make the line number automatically preceded by a 0, unifying the output format

Command:

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

Description:

The nl -b a -n rz command line number defaults to six bits, to adjust the number of bits you can add the parameter -w 3 to adjust to 3 bits.

Reference:

www.cnblogs.com/peida/archive/2012/11/01/2749048.html

comments powered by Disqus