nl命令在linux系統(tǒng)中用來計(jì)算文件中行號。nl可以將輸出的文件內(nèi)容自動的加上行號!其默認(rèn)的結(jié)果與 cat -n
有點(diǎn)不太一樣, nl
可以將行號做比較多的顯示設(shè)計(jì),包括位數(shù)與是否自動補(bǔ)齊 0
等等的功能。
nl
[選項(xiàng)]… [文件]…
-b
:指定行號指定的方式,主要有兩種:-b a
:表示不論是否為空行,也同樣列出行號(類似 cat -n
);-b t
:如果有空行,空的那一行不要列出行號(默認(rèn)值);-n
:列出行號表示的方法,主要有三種:-n ln
:行號在螢?zāi)坏淖钭蠓斤@示;-n rn
:行號在自己欄位的最右方顯示,且不加 0 ;-n rz
:行號在自己欄位的最右方顯示,且加 0 ;-w
:行號欄位的占用的位數(shù)。-p
在邏輯定界符處不重新開始計(jì)算。 nl
命令讀取 File
參數(shù)(缺省情況下標(biāo)準(zhǔn)輸入),計(jì)算輸入中的行號,將計(jì)算過的行號寫入標(biāo)準(zhǔn)輸出。 在輸出中,nl
命令根據(jù)您在命令行中指定的標(biāo)志來計(jì)算左邊的行。 輸入文本必須寫在邏輯頁中。每個邏輯頁有頭、主體和頁腳節(jié)(可以有空節(jié))。 除非使用 -p
標(biāo)志,nl
命令在每個邏輯頁開始的地方重新設(shè)置行號??梢詥为?dú)為頭、主體和頁腳節(jié)設(shè)置行計(jì)算標(biāo)志(例如,頭和頁腳行可以被計(jì)算然而文本行不能)。
用 nl
列出 log.log
的內(nèi)容
命令:
nl log.log
輸出:
[yiibai@localhost test]$ cat log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.
this is line 5.
-----------------end
[yiibai@localhost test]$ nl log.log
1 this is line 1.
2 this is line 2.
3 this is line 3.
4 this is line 4.
5 this is line 5.
6 -----------------end
[yiibai@localhost test]$
說明:文件中的空白行,
nl
不會加上行號。
用 nl
列出 log.log
的內(nèi)容,空本行也加上行號。
命令:
nl -b a log.log
輸出:
[yiibai@localhost test]$ nl -b a log.log
1 this is line 1.
2 this is line 2.
3 this is line 3.
4 this is line 4.
5
6 this is line 5.
7
8 -----------------end
[yiibai@localhost test]$
讓行號前面自動補(bǔ)上0
,統(tǒng)一輸出格式。
[yiibai@localhost test]$ nl -b a -n rz log.log
000001 this is line 1.
000002 this is line 2.
000003 this is line 3.
000004 this is line 4.
000005
000006 this is line 5.
000007
000008 -----------------end
[yiibai@localhost test]$ nl -b a -n rz -w 3 log.log
001 this is line 1.
002 this is line 2.
003 this is line 3.
004 this is line 4.
005
006 this is line 5.
007
008 -----------------end
[yiibai@localhost test]$
說明:nl -b a -n rz
命令行號默認(rèn)為六位,要調(diào)整位數(shù)可以加上參數(shù) -w 3
調(diào)整為3
位。