nl (UNIX)

nl
対応OS Unix and Unix 系
プラットフォーム クロスプラットフォーム
種別 コマンド
ライセンス coreutils: GPLv3+
テンプレートを表示

nl は、ファイルまたは標準入力からの入力に行番号を添えて標準出力に出力する Unix コマンドである。

歴史[編集]

nl が登場したのは System V release 2 である[1]

このコマンドは 1987 年の第 2 版より X/Open Portability Guide の一部となっている。これは初版の POSIX.1Single Unix Specification に引き継がれた[2]

GNU coreutils に含まれている nl は Scott Bartram と David MacKenzie によって書かれたものである[3]

[編集]

以下はファイル 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 

関連項目[編集]

脚注・出典[編集]

  1. ^ nl(1) – FreeBSD General Commands Manual Pages (en)
  2. ^ nl – The Open Group基本仕様書第7号2018年版「シェル及びユーティリティ」
  3. ^ nl(1)Linux General Commands Manual (en)