LoginSignup
2
0

More than 3 years have passed since last update.

行番号を付加する空行の単位を変更するnlコマンドのlオプション

Posted at

nlコマンドはlオプションを指定することで、行番号を付加する空行の単位を変更することができます。

lオプションの使い方

-lのあとに数値を指定します。デフォルトは1です。-b -h -f オプションの付番方式にa(すべての行に番号を振る)を使う必要があり、これも同時に指定します。

info nl (GNU coreutils)

‘-l NUMBER’
‘--join-blank-lines=NUMBER’
     Consider NUMBER (default 1) consecutive empty lines to be one
     logical line for numbering, and only number the last one.  Where
     fewer than NUMBER consecutive empty lines occur, do not number
     them.  An empty line is one that contains no characters, not even
     spaces or tabs.

ここにきて「空行とは何か」の説明がありますね。タブもスペースも含まない行が空行です。

man nl (BSD)

     -l num       If numbering of all lines is specified for the current logical
                  section using the corresponding -b a, -f a or -h a option, spec-
                  ify the number of adjacent blank lines to be considered as one.
                  For example, -l 2 results in only the second adjacent blank line
                  being numbered.  The default num value is 1.

POSIX

-l num
Specify the number of blank lines to be considered as one. For example, -l 2 results in only the second adjacent blank line being numbered (if the appropriate -h a, -b a, or -f a option is set). The default shall be 1.

lオプションを試す

単純なデータで試してみましょう。

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




b


c

$ echo -e 'a\n\n\n\n\nb\n\n\nc\n' | nl 
     1  a




     2  b


     3  c

$ echo -e 'a\n\n\n\n\nb\n\n\nc\n' | nl -b a
     1  a
     2  
     3  
     4  
     5  
     6  b
     7  
     8  
     9  c
    10  
$ echo -e 'a\n\n\n\n\nb\n\n\nc\n' | nl -b a -l 2
     1  a

     2  

     3  
     4  b

     5  
     6  c

正直なところ何に使うオプションなのか検討つきません。私は-lを今まで使ったことがありません。


ToDo

なんとかユースケースを考えたいのですが思いつきませんでした。何か思いついたら追記します。
連続したn個の空行で1つの空行としてみなすマークアップ言語などがあるのかもしれませんね。

2
0
1

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
2
0