gpt4 book ai didi

awk - 为什么 GNU Awk 的 POSIX 模式在将 RS 设置为另一件事时不将新行视为字段?

转载 作者:行者123 更新时间:2023-12-04 15:10:17 25 4
gpt4 key购买 nike

我正在通过 GNU Awk User's Guide并在 4.1.1 Record Splitting with Standard awk 中找到了这个部分:

When using regular characters as the record separator, there is one unusual case that occurs when gawk is being fully POSIX-compliant (see section Command-Line Options). Then, the following (extreme) pipeline prints a surprising ‘1’:

$ echo | gawk --posix 'BEGIN { RS = "a" } ; { print NF }'
-| 1

There is one field, consisting of a newline. The value of the built-in variable NF is the number of fields in the current record. (In the normal case, gawk treats the newline as whitespace, printing ‘0’ as the result. Most other versions of awk also act this way.)


我检查了它,但它在我的 GNU Awk 5.0.0 上对我不起作用:
$ gawk --version
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
$ echo | gawk --posix 'BEGIN { RS = "a" } ; { print NF }'
0
也就是说,行为与没有 POSIX 模式时完全相同:
$ echo | gawk 'BEGIN { RS = "a" } ; { print NF }'
0
我理解它提出的观点,当记录分隔符不是默认值(即,它不是新行)时,仅将新行的内容视为一个字段。但是,我无法重现它。
我应该如何重现这个例子?我也试过 gawk --traditionalgawk -P但结果总是0。
由于我检查的 GNU Awk 用户指南适用于 5.1 版本并且我有 5.0.0,因此我还检查了 an archived version for 5.0.0它显示了相同的行,所以它在 5.0 和 5.1 之间没有变化。

最佳答案

在阅读 POSIX 标准时,我们发现:

The awk utility shall interpret each input record as a sequence of fields where, by default, a field is a string of non-<blank> non-<newline> characters. This default <blank> and <newline> field delimiter can be changed by using the FS built-in variable

If FS is <space>, skip leading and trailing <blank> and <newline> characters; fields shall be delimited by sets of one or more <blank> or <newline> characters.

source: POSIX awk standard: IEEE Std 1003.1-2017


话虽如此,正确的行为应该如下:
$ echo | awk 'BEGIN{RS="a"}{print NR,NF,length}'
1 0 1
  • 单个记录:没有遇到 字符
  • 无字段:FS是默认空格,因此所有前导和尾随 字符;被跳过
  • 长度一:记录中只有一个字符。

  • 定义 FS 时,故事完全不同:
    $ echo | awk 'BEGIN{FS="b";RS="a"}{print NR,NF,length}'
    1 1 1
    $ echo | awk 'BEGIN{FS="\n";RS="a"}{print NR,NF,length}'
    1 2 1

    总之:我相信 GNU awk 文档是错误的。

    25 4 0
    文章推荐: javascript - 子元素在shadow dom中的使用