nl命令
nl命令在linux系統中用來計算文件中行號。nl可以將輸出的文件內容自動的加上行號!其默認的結果與 cat -n 有點不太一樣, nl 可以將行號做比較多的顯示設計,包括位數與是否自動補齊 0 等等的功能。
1.命令格式
nl [選項]… [文件]…
2.命令參數
-
-b:指定行號指定的方式,主要有兩種: -
-b a:表示不論是否爲空行,也同樣列出行號(類似cat -n); -
-b t:如果有空行,空的那一行不要列出行號(默認值); -
-n:列出行號表示的方法,主要有三種: -
-n ln:行號在螢幕的最左方顯示; -
-n rn:行號在自己欄位的最右方顯示,且不加 0 ; -
-n rz:行號在自己欄位的最右方顯示,且加 0 ; -
-w:行號欄位的佔用的位數。 -
-p在邏輯定界符處不重新開始計算。
3.命令功能
nl 命令讀取 File 參數(缺省情況下標準輸入),計算輸入中的行號,將計算過的行號寫入標準輸出。 在輸出中,nl 命令根據您在命令行中指定的標誌來計算左邊的行。 輸入文本必須寫在邏輯頁中。每個邏輯頁有頭、主體和頁腳節(可以有空節)。 除非使用 -p 標誌,nl 命令在每個邏輯頁開始的地方重新設置行號。可以單獨爲頭、主體和頁腳節設置行計算標誌(例如,頭和頁腳行可以被計算然而文本行不能)。
4.使用實例
實例一
用 nl 列出 log.log 的內容
命令:
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 的內容,空本行也加上行號。
命令:
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]$
實例三
讓行號前面自動補上0,統一輸出格式。
[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 命令行號默認爲六位,要調整位數可以加上參數 -w 3 調整爲3位。