LoginSignup
0
0

More than 3 years have passed since last update.

行番号の付番方式を変更するnlコマンドのb,f,hオプション

Last updated at Posted at 2020-12-09

nlコマンドはbオプションやf,hオプションを指定することで、論理ページのセクションごとに付番方式を変更することができます。論理ページについては前の記事をご覧ください。

b,f,hオプションの使い方

ーbが「ボディ」、-fが「フッター」、-hが「ヘッダー」をそれぞれ制御しますが、これら3つのオプションの使い方は同じです。付番方式は次の4つの中から指定できます。

オプション 付番方式
a すべての行に番号を振る
t 空行以外に番号を振る
n 行番号を振らない
p 正規表現にマッチする行に番号を振る

オプションは-b aというように組み合わせて指定します。なお、4つ目の正規表現についてはこの記事では触れません。

nlがデフォルトで空行に番号を振らないことは前の記事でも触れましたが、各セクションごとのデフォルトは、ボディがt「空行以外に番号を振る」、ヘッダーとフッターがn「行番号を振らない」になっています。aを指定することで、他のコマンドのようにすべての行に番号を振るよう変更できます。また、nlに行番号を振らない付番方式の指定があるのは少し不思議な感じがしますが、これは一部の行を付番から除外するために必要な方式ということです。

マニュアルも確認していきましょう。

info nl (GNU coreutils)

‘-b STYLE’
‘--body-numbering=STYLE’
     Select the numbering style for lines in the body section of each
     logical page.  When a line is not numbered, the current line number
     is not incremented, but the line number separator character is
     still prepended to the line. The styles are:

     ‘a’
          number all lines,
     ‘t’
          number only nonempty lines (default for body),
     ‘n’
          do not number lines (default for header and footer),
     ‘pBRE’
          number only lines that contain a match for the basic regular
          expression BRE.  *Note Regular Expressions: (grep)Regular
          Expressions.

‘-f STYLE’
‘--footer-numbering=STYLE’
     Analogous to ‘--body-numbering’.

‘-h STYLE’
‘--header-numbering=STYLE’
     Analogous to ‘--body-numbering’.

coreutilsのnlでは付番方式を「スタイル」と呼んでいます。

man nl (BSD)

     -b type      Specify the logical page body lines to be numbered.  Recognized
                  type arguments are:

                  a       Number all lines.

                  t       Number only non-empty lines.

                  n       No line numbering.

                  pexpr   Number only those lines that contain the basic regular
                          expression specified by expr.

                  The default type for logical page body lines is t.

     -f type      Specify the same as -b type except for logical page footer
                  lines.  The default type for logical page footer lines is n.

     -h type      Specify the same as -b type except for logical page header
                  lines.  The default type for logical page header lines is n.

一方、BSD系のnlでは付番方式を「タイプ」と呼んでいます。

busybox nl --help

    -b STYLE    Which lines to number - a: all, t: nonempty, n: none

論理ページをサポートしないbusyboxのnlですが、-bだけが存在します。つまり、データを単一のボディとみなして付番方式を変更することができます。そして方式は正規表現を除く3つです。

POSIX

-b type
Specify which logical page body lines shall be numbered. Recognized types and their meaning are:
a
Number all lines.
t
Number only non-empty lines.
n
No line numbering.
pstring
Number only lines that contain the basic regular expression specified in string.
The default type for logical page body shall be t (text lines numbered).

-f type
Specify the same as b type except for footer. The default for logical page footer shall be n (no lines numbered).
-h type
Specify the same as b type except for header. The default type for logical page header shall be n (no lines numbered).

どちらが正しいということはないですが、POSIXでは付番方式を「タイプ」と呼ぶようです。

bオプションを試す

まずはボディのセクションに対して付番方式をそれぞれ試してみます。

$ echo -e 'a\n\nb' 
a

b
$ echo -e 'a\n\nb' | nl -b a
     1  a
     2  
     3  b
$ echo -e 'a\n\nb' | nl -b t
     1  a

     2  b
$ echo -e 'a\n\nb' | nl -b n
        a

        b

f,hオプションを試す

各セクションをまとめて試してみます。

coreutils
$ echo -e '\:\na\n\:\:\nb\n\:\:\:\nc' 
\:
a
\:\:
b
\:\:\:
c
$ echo -e '\:\na\n\:\:\nb\n\:\:\:\nc' | nl -b a -f a -h a

     1  a

     1  b

     1  c
$ echo -e '\:\na\n\:\:\nb\n\:\:\:\nc' | nl -b n -f n -h n

       a

       b

       c
BSD
$ echo -e '\:\na\n\:\:\nb\n\:\:\:\nc' | nl -b a -f a -h a
     1  a
     1  b
     1  c
$ echo -e '\:\na\n\:\:\nb\n\:\:\:\nc' | nl -b n -f n -h n
        a
        b
        c

coreutilsとBSD系で若干の差異がありますが、空行に付加されるデータ(前の記事)の違いと、セクションの区切り文字列を空行に変換する(前の記事)かどうかの違いだけです。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0