Linux 命令(114) nl 命令

2021-10-02 16:05:15 字數 2361 閱讀 3780

nl(number line)命令用於計算檔案的行號並將帶有行號的內容輸出到標準輸出。相比於命令cat -n,nl 可以對行號做比較多的顯示設計,包括位數與是否自動補齊 0 等功能。

nl [option]... [file]...
在沒有檔案或檔案是 - 時,從標準輸入讀取內容。

注意,長選項的強制性引數對於短選項也是強制的。

-b, --body-numbering=style

使用指定樣式給檔案的正文行編號。style 可取值如下:

a:給所有行編號,不論其是否為空行(類似 cat -n)

t:只給非空行編號

n:不編行

pbre:只給包含基本正規表示式(basic regular expression,bre)的行編號

-d, --section-delimiter=cc

使用 cc 作為邏輯頁分隔符

-f, --footer-numbering=style

使用指定樣式給檔案的頁腳行編號。style 可取值同上

-h, --header-numbering=style

使用指定樣式給檔案的頁腳行編號。style 可取值同上

-i, --line-increment=number

使用指定增量增加行號

-l, --join-blank-lines=number

將指定數量的一組空行視為 1 行

-n, --number-format=format

指定行號格式,format 主要有三種:

ln:左對齊,無前導 0

rn:右對齊,無前導 0

rz:右對齊,有前導 0

-p, --no-renumber

不要重置每個區段的行號

-s, --number-separator=string

可能的話在行號後新增字串

-v, --starting-line-number=number

設定每個區段的第一行的行號

-w, --number-width=number

行號欄位的占用的位數

--help

顯示此幫助並退出

--version

顯示版本資訊並退出

nl 不帶任何選項執行時,使用如下預設選項:

-bt -d'\:' -fn -hn -i1 -l1 -n'rn' -s-v1 -w6
cc 是用於分隔邏輯頁數的兩個分界符,如果要指定 「」, 請輸入 「\」。

給定檔案 test.txt 用於測試,內容如下:

testtxt1

testtxt2

testtxt3

(1)使用預設選項給 test.txt 檔案編號並輸出。

nl test.txt

1 testtxt1

2 testtxt2

3 testtxt3

(2)顯示行號,指定對齊方式。

# 左對齊,無前導 0

nl -nln test.txt

1 testtxt1

2 testtxt2

3 testtxt3

# 右對齊,無前導 0

nl -nrn test.txt

1 testtxt1

2 testtxt2

3 testtxt3

# 右對齊,有前導 0

nl -nrz test.txt

000001 testtxt1

000002 testtxt2

000003 testtxt3

(3)指定寬度為 4 位,預設為 6 位。

nl -nrz -w4 test.txt

0001 testtxt1

0002 testtxt2

0003 testtxt3

(4)空行顯示行號。

nl -nrz -w4 -ba test.txt

0001 testtxt1

0002 testtxt2

0003

0004 testtxt3

(5)設定行號增量為 2。

nl -nrz -w4 -ba -i2 test.txt

0001 testtxt1

0003 testtxt2

0005

0007 testtxt3

[1] nl(1) manual

[2] .每天乙個linux命令(11):nl命令

Linux命令之nl命令

linux命令之nl命令 nl 命令在 linux 系統中用來計算檔案中行號。nl 可以將輸出的檔案內容自動的加上行號,其預設的結果和 與 cat n 有點不太一樣,nl 可以將行號做比較多的顯示設計,包括位數是否自動補齊 0 等等的功能 1.命令格式 1nl 選項 檔案 2.命令引數 b 指定行號...

Linux命令(11) nl命令

nl命令在 linux 系統中用來計算檔案中行號 nl 可以將輸出的檔案內容自動的加上行號!其預設的結果與 cat n 有點不太一樣,nl 可以將行號做比較多的顯示設計,包括位數與是否自動補齊 0 等等的功能。1 命令格式 nl 選項 檔案 2 命令引數 b 指定行號指定的方式,主要有兩種 b a ...

Linux檔案目錄命令 nl命令

nl命令在linux系統中用來計算檔案中行號。nl 可以將輸出的檔案內容自動的加上行號!其預設的結果與 cat n 有點不太一樣,nl 可以將行號做比較多的顯示設計,包括位數與是否自動補齊 0 等等的功能。用法 nl 選項 檔案 將指定的各個檔案新增行號標註後寫到標準輸出。如果不指定檔案或指定檔案為...