nl コマンド

nlコマンドはテキストに行番号をつけて出力するコマンド。

apple
banana
cherry
grape
melon

上記のsample.txtnlコマンドを実行すると以下のように行番号が付加される。

$ nl sample.txt
     1  apple
     2  banana
     3  cherry
     4  grape
     5  melon

パイプで標準入力から受け取る場合も同様に動作する。

$ cat sample.txt | nl
     1  apple
     2  banana
     3  cherry
     4  grape
     5  melon

コマンドの出力結果に行番号をつける場合に便利。

空行の扱い

デフォルトでは空行には行番号がつかない。

apple

banana

cherry
$ nl blank.txt
     1  apple

     2  banana

     3  cherry

空行には番号がつかず、番号のカウントもされない。

すべての行に番号をつける(-b a)

-b aオプションを使うと、空行を含むすべての行に番号をつける。

$ nl -b a blank.txt
     1  apple
     2
     3  banana
     4
     5  cherry

空行にも番号がつき、カウントされる。

番号を表示しない(-b n)

-b nオプションを使うと、すべての行で番号を表示しない。

$ nl -b n blank.txt
       apple

       banana

       cherry

行番号が表示されない。

正規表現で番号をつける行を指定(-b p)

-b p"パターン"オプションを使うと、正規表現に一致する行だけに番号をつける。

$ nl -b p"^a" sample.txt
     1  apple
       banana
       cherry
       grape
       melon

^aは行頭がaで始まる行にマッチする。appleだけに番号がつく。

$ nl -b p"e$" sample.txt
     1  apple
       banana
       grape
       melon

e$は行末がeで終わる行にマッチする。appleとgrapeに番号がつく。

番号のフォーマットを変更する(-n)

-nオプションで行番号のフォーマットを指定できる。

左寄せ(-n ln)

$ nl -n ln sample.txt
1       apple
2       banana
3       cherry
4       grape
5       melon

行番号が左寄せで表示される。

右寄せ(-n rn)

$ nl -n rn sample.txt
     1  apple
     2  banana
     3  cherry
     4  grape
     5  melon

デフォルトで右寄せである。明示的に指定する場合は-n rnを使う。

ゼロ埋め(-n rz)

$ nl -n rz sample.txt
000001  apple
000002  banana
000003  cherry
000004  grape
000005  melon

行番号がゼロ埋めで表示される。

行番号の桁数を指定する(-w)

-wオプションで行番号の表示幅を指定できる。

$ nl -w 3 sample.txt
  1  apple
  2  banana
  3  cherry
  4  grape
  5  melon

行番号が3桁で表示される。

$ nl -w 10 sample.txt
         1  apple
         2  banana
         3  cherry
         4  grape
         5  melon

行番号が10桁で表示される。

ゼロ埋めと組み合わせると、桁数を揃えやすい。

$ nl -n rz -w 4 sample.txt
0001  apple
0002  banana
0003  cherry
0004  grape
0005  melon

4桁でゼロ埋めされた行番号が表示される。

区切り文字を指定する(-s)

-sオプションで行番号とテキストの間の区切り文字を指定できる。

$ nl -s ": " sample.txt
     1: apple
     2: banana
     3: cherry
     4: grape
     5: melon

行番号とテキストの間に: が挿入される。

$ nl -s " -> " sample.txt
     1 -> apple
     2 -> banana
     3 -> cherry
     4 -> grape
     5 -> melon

->を区切り文字として使える。

左寄せと組み合わせると、見やすい出力になる。

$ nl -n ln -s ". " sample.txt
1. apple
2. banana
3. cherry
4. grape
5. melon

番号付きリストのような出力が得られる。

開始番号を指定する(-v)

-vオプションで開始番号を指定できる。

$ nl -v 10 sample.txt
    10  apple
    11  banana
    12  cherry
    13  grape
    14  melon

行番号が10から始まる。

増分を指定する(-i)

-iオプションで行番号の増分を指定できる。

$ nl -i 10 sample.txt
     1  apple
    11  banana
    21  cherry
    31  grape
    41  melon

行番号が10ずつ増える。

開始番号と組み合わせると柔軟な番号付けができる。

$ nl -v 100 -i 10 sample.txt
   100  apple
   110  banana
   120  cherry
   130  grape
   140  melon

100から始まり、10ずつ増える行番号が付与される。

複数オプションの組み合わせ

複数のオプションを組み合わせて、柔軟な行番号付けができる。

$ nl -n ln -s ". " -v 1 -i 1 -w 2 -n rz -b a sample.txt
01. apple
02. banana
03. cherry
04. grape
05. melon

左寄せ、ドット区切り、1から開始、1ずつ増分、2桁表示、すべての行に番号付与の組み合わせである。

具体例

コマンドの出力に行番号をつける

$ ls -1 /etc | rl -n ln -s ". " | head -5
     1. afpovertcp.cfg
     2. aliases
     3. aliases.db
     4. apache2
     5. asl

ディレクトリの一覧に番号付きリスト形式で番号をつける。

ログファイルの特定行を確認する

$ nl -v 1 -w 5 /var/log/syslog | grep "error"
  123  Jan 12 10:23:45 server kernel: [error] disk error
  456  Jan 12 11:45:12 server kernel: [error] memory error

ログファイルに行番号をつけて、エラー行の位置を確認できる。

空行をスキップして行番号をつける

$ cat config.txt
# Configuration file

server=localhost

port=8080
$ nl -b t config.txt
     1  # Configuration file

     2  server=localhost

     3  port=8080

空行をスキップして、テキスト行だけに番号がつく。デフォルトの-b t動作である。

プログラムのコード行数をカウントする

$ nl -b a program.py | tail -1
   150      return result

最後の行番号を確認することで、ファイルの総行数がわかる。

CSVファイルにレコード番号をつける

name,age,city
Alice,25,Tokyo
Bob,30,Osaka
Charlie,35,Nagoya
$ nl -n ln -s "," -v 1 data.csv
1,name,age,city
2,Alice,25,Tokyo
3,Bob,30,Osaka
4,Charlie,35,Nagoya

CSVファイルの各行にレコード番号を追加できる。

cat -n との違い

cat -nコマンドも行番号をつけられるが、nlコマンドの方が柔軟である。

参考:シェルスクリプトでテキストの先頭に連番をつける(cat -n)

$ cat -n sample.txt
     1  apple
     2  banana
     3  cherry
     4  grape
     5  melon

cat -nは常にすべての行に番号をつける。

nlコマンドは以下の点でcat -nより柔軟である。

  • 空行の扱いを制御できる(-bオプション)
  • 番号のフォーマットを変更できる(-nオプション)
  • 桁数を指定できる(-wオプション)
  • 区切り文字を指定できる(-sオプション)
  • 開始番号を指定できる(-vオプション)
  • 増分を指定できる(-iオプション)
  • 正規表現で番号をつける行を選択できる(-b pオプション)

シンプルな行番号付けはcat -nで十分だが、細かい制御が必要な場合はnlコマンドを使う。