nl は、ファイルまたは標準入力からの入力に行番号を添えて標準出力に出力する Unix コマンドである。
歴史 nl が登場したのは System V release 2 である。
このコマンドは 1987 年の第 2 版より X/Open Portability Guide の一部となっている。これは初版の POSIX.1 と Single Unix Specification に引き継がれた。
GNU coreutils に含まれている nl は Scott Bartram と David MacKenzie によって書かれたものであるnl(1) – Linux General Commands Manual (en)。
例 以下はファイル filename の行を表示する例と、ps コマンドの出力をパイプで渡す例である。$ nl filename 1 hoge 2 fuga
3 piyo 4 hogehoge $ ps | nl 1 PID TTY TIME CMD 2 930631 pts/1 00:00:00 bash 3 2077186 pts/1 00:00:00 ps 4 2077188 pts/1 00:00:00 nl次に示す例は、文字 A から始まる行 (正規表現 /^A/ にマッチする行) のみを数え上げる。コマンド中の filename は省略可能である。$ nl -b p^A filename apple 1 Apple BANANA 2 Allspice strawberry以下のようにすると grep -n の代わりとして利用できる。$ cat somefile aaaa bbbb cccc dddc $ nl somefile | grep cccc 3 cccc
関連項目
wc (Unix) – 改行数、単語数、バイト数を表示するコマンドコマンド cat (Unix) – ファイルを連結するコマンド (cat -n と実行すると nl のように行番号を表示する実装もある) UNIX ユーティリティの一覧
脚注・出典