画像提供:マイナビニュース

写真拡大

HowtoForgeに3月7日(米国時間)に掲載された記事「Linux nl Command Tutorial for Beginners (7 Examples)」が、nlコマンドの使い方を伝えた。nlコマンドはテキストデータに行数を付与するコマンド。オプションで、空行の扱いや始まりの行数、増加数など、挙動を変更することができる。

紹介されている主な使い方は次のとおり。

○nlコマンドの基本的な使い方(空行には行番号がつかない)

# nl /etc/libaudit.conf

1 # This is the configuration file for libaudit tunables.

2 # It is currently only used for the failure_action tunable.

3 # failure_action can be: log, ignore, terminate

4 failure_action = ignore

#

○-b aで空行にも行番号を付ける

# nl -b a /etc/libaudit.conf

1 # This is the configuration file for libaudit tunables.

2 # It is currently only used for the failure_action tunable.

3

4 # failure_action can be: log, ignore, terminate

5 failure_action = ignore

6

7

#

○-iでインクリメント行数を指定

# nl -i 10 /etc/libaudit.conf

1 # This is the configuration file for libaudit tunables.

11 # It is currently only used for the failure_action tunable.

21 # failure_action can be: log, ignore, terminate

31 failure_action = ignore

#

○-lで指定した連続した空行数を1つの行として扱う

# nl -b a -l 2 /etc/libaudit.conf

1 # This is the configuration file for libaudit tunables.

2 # It is currently only used for the failure_action tunable.

3 # failure_action can be: log, ignore, terminate

4 failure_action = ignore

5

#

○-nで行番号表示形式を指定。lnで左そろえ、rnで右そろえ、rzでゼロパティング出力

# nl -n ln /etc/libaudit.conf

1 # This is the configuration file for libaudit tunables.

2 # It is currently only used for the failure_action tunable.

3 # failure_action can be: log, ignore, terminate

4 failure_action = ignore

# nl -n rn /etc/libaudit.conf

1 # This is the configuration file for libaudit tunables.

2 # It is currently only used for the failure_action tunable.

3 # failure_action can be: log, ignore, terminate

4 failure_action = ignore

# nl -n rz /etc/libaudit.conf

000001 # This is the configuration file for libaudit tunables.

000002 # It is currently only used for the failure_action tunable.

000003 # failure_action can be: log, ignore, terminate

000004 failure_action = ignore

#

○-sでセパレータをタブから指定した文字列へ変更

# nl -s ' [===] ' /etc/libaudit.conf

1 [===] # This is the configuration file for libaudit tunables.

2 [===] # It is currently only used for the failure_action tunable.

3 [===] # failure_action can be: log, ignore, terminate

4 [===] failure_action = ignore

#

○-vで開始行数を指定

# nl -v 10 /etc/libaudit.conf

10 # This is the configuration file for libaudit tunables.

11 # It is currently only used for the failure_action tunable.

12 # failure_action can be: log, ignore, terminate

13 failure_action = ignore

#

nlコマンドはAT&T System V Release 2 UNIXには登場していたコマンド。初期から存在している古いコマンドだが、あまり使い方が知られていない側面もある。行数を付与する方法として、catコマンドに-nオプションを指定するなどの方法もある。