くんすとの備忘録

IT系技術メモ

移転しました。

15秒後に自動的にリダイレクトします。

【FreeBSD】nlコマンドのオプション、論理ページなど

昨日のシェル芸ヴェンキョウカイ(問5)で話題(?)になった、nlコマンドのオプションについて。

これもさらりとオプションの再確認だけ……
と思っていたのですが、『論理ページ』なるものが気になったのでむしろそちらをメインに確認してきます。

論理ページのセクションの区切り文字について

  • \:\:\: … ヘッダ
  • \:\: … 本文
  • \; … フッタ

テストデータ

今回は論理ページの区切りを仕込んだ手作成のデータを使用します。

% cat nldata
\:\:\:
this is header line 1
this is header line 2
\:\:
this is main section.

aaaaa
bbbbb

after 1 blank cccc
\:\:
this is second main section.
ddddd


after 2 blank eeeeeeee
fffffff
gggg
\:
this is footer line 1
this is footer line 2

このデータは、

  • ヘッダ…2行
  • 本文1…6行
  • 本文2…7行

という構成になっています。

オプションの確認

オプションなし

まずは、オプションなしで実行してみます。

% nl nldata
        this is header line 1
        this is header line 2
     1  this is main section.

     2  aaaaa
     3  bbbbb

     4  after 1 blank cccc
     1  this is second main section.
     2  ddddd


     3  after 2 blank eeeeeeee
     4  fffffff
     5  gggg
        this is footer line 1
        this is footer line 2

ヘッダとフッタには番号が振られず、また、本文はセクションごとに番号が振りなおされています。
また、セクションの区切り文字は表示されていません。

なるほど、nlってこういうことをするためのコマンドだったのね・・・

行番号を付けたり消したりするオプション

  • -b a 「本文の」行全部に行番号をつける
% nl -b a nldata
        this is header line 1
        this is header line 2
     1  this is main section.
     2
     3  aaaaa
     4  bbbbb
     5
     6  after 1 blank cccc
     1  this is second main section.
     2  ddddd
     3
     4
     5  after 2 blank eeeeeeee
     6  fffffff
     7  gggg
        this is footer line 1
        this is footer line 2
  • -h a 「ヘッダの」行全部に行番号をつける
% nl -h a nldata
     1  this is header line 1
     2  this is header line 2
     1  this is main section.

     2  aaaaa
     3  bbbbb

     4  after 1 blank cccc
     1  this is second main section.
     2  ddddd


     3  after 2 blank eeeeeeee
     4  fffffff
     5  gggg
        this is footer line 1
        this is footer line 2
  • -f a 「フッタの」行全部に行番号をつける
% nl -f a nldata
        this is header line 1
        this is header line 2
     1  this is main section.

     2  aaaaa
     3  bbbbb

     4  after 1 blank cccc
     1  this is second main section.
     2  ddddd


     3  after 2 blank eeeeeeee
     4  fffffff
     5  gggg
     1  this is footer line 1
     2  this is footer line 2
  • -p セクションを超えても連番を保持する
% nl -p nldata
        this is header line 1
        this is header line 2
     1  this is main section.

     2  aaaaa
     3  bbbbb

     4  after 1 blank cccc
     5  this is second main section.
     6  ddddd


     7  after 2 blank eeeeeeee
     8  fffffff
     9  gggg
        this is footer line 1
        this is footer line 2
% nl -p -h a -b n -f a nldata
     1  this is header line 1
     2  this is header line 2
        this is main section.

        aaaaa
        bbbbb

        after 1 blank cccc
        this is second main section.
        ddddd


        after 2 blank eeeeeeee
        fffffff
        gggg
     3  this is footer line 1
     4  this is footer line 2

※ヘッダとフッタに連続で採番し、本文には採番しない

行番号の採番方法を変える

  • -i num 行番号の増分値を指定
% nl -i 2 nldata
        this is header line 1
        this is header line 2
     1  this is main section.

     3  aaaaa
     5  bbbbb

     7  after 1 blank cccc
     1  this is second main section.
     3  ddddd


     5  after 2 blank eeeeeeee
     7  fffffff
     9  gggg
        this is footer line 1
        this is footer line 2
  • -v num 行番号の初期値を指定
% nl -v 25 nldata
        this is header line 1
        this is header line 2
    25  this is main section.

    26  aaaaa
    27  bbbbb

    28  after 1 blank cccc
    25  this is second main section.
    26  ddddd


    27  after 2 blank eeeeeeee
    28  fffffff
    29  gggg
        this is footer line 1
        this is footer line 2

行番号の表示方法を変更

  • -n format
    • ln 左詰め
    • rn 右詰め
    • rz 右詰め(左0あり)
% nl -n rz nldata
        this is header line 1
        this is header line 2
000001  this is main section.

000002  aaaaa
000003  bbbbb

000004  after 1 blank cccc
000001  this is second main section.
000002  ddddd


000003  after 2 blank eeeeeeee
000004  fffffff
000005  gggg
        this is footer line 1
        this is footer line 2
  • -s 文字 行番号とテキストの間の区切り文字を指定。初期値はtab。
% nl -s , nldata
      ,this is header line 1
      ,this is header line 2
     1,this is main section.
      ,
     2,aaaaa
     3,bbbbb
      ,
     4,after 1 blank cccc
     1,this is second main section.
     2,ddddd
      ,
      ,
     3,after 2 blank eeeeeeee
     4,fffffff
     5,gggg
      ,this is footer line 1
      ,this is footer line 2
  • -w num 行番号エリアの幅を指定。
% nl -w 3 -s : -n rz nldata
   :this is header line 1
   :this is header line 2
001:this is main section.
   :
002:aaaaa
003:bbbbb
   :
004:after 1 blank cccc
001:this is second main section.
002:ddddd
   :
   :
003:after 2 blank eeeeeeee
004:fffffff
005:gggg
   :this is footer line 1
   :this is footer line 2


「-d」オプションだけは試してみたけどよくわからなかった……