LoginSignup
0
0

More than 3 years have passed since last update.

gawk で NFに入る最大値を調べてみた。

Last updated at Posted at 2020-07-27

きっかけ

以前、以下のようなファイルを、

sample.txt
1 2 3 4 5 6 7 8 9

以下のように3桁づつ改行を入れたくなるときがあり、

1 2 3
4 5 6
7 8 9

以下のような、awkスクリプトを書きました。

{
    for(i=1; i<=NF; i++) {
        printf "%d ", $i;
        if((i % 3) == 0) print "";
    }
}

「あれ、これって入力ファイルがいくつまで対応できるのでしょう?」とふと思いました。
こんなファイルでも大丈夫なのかなと。。。

sample.txt
1 2 3 4 5 6 7 8 9 ... /* このあと大量に数字がつづく*/ .... 9999999999999999999

調査(ドキュメント)

ドキュメントの NF の記載になにか書かれているかも。

NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record. No matter how many fields there are, the last field in a record can be represented by \$NF. So, \$NF is the same as \$7, which is ‘example.’. If you try to reference a field beyond the last one (such as $8 when the record has only seven fields), you get the empty string. (If used in a numeric operation, you get zero.)

「NFに入る最大値は・・・」みたいな記載を期待したが、特になし。

調査(ググる)

ここに、気になる記述が。

Correct. According to info gawk: "The internal representation of all numbers, including integers, uses double-precision floating-point numbers. On most modern systems, these are in IEEE 754 standard format."

たしかに、ドキュメントにも記載がある。

ということで、最大どこまで?

ヒントになりそうなページがあった。

気が向いたら、もう少し調べる。

追記(2020/08/04)

ときどきの雑記帖 Re* にてコメントをいただけていた。
ありがとうございます。

実はマニュアル The GNU Awk User's Guide にこの辺の制限は明記されている (バージョン5になるまではマニュアルではなくソースアーカイブ中のLIMITATIONSというファイルに記載)。

ホントだ。(気づかなかった:sweat:)
https://www.gnu.org/software/gawk/manual/gawk.html#Implementation-Limitations

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