- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过 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 }'
-| 1There 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.)
$ 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 --traditional
或
gawk -P
但结果总是0。
最佳答案
在阅读 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 variableIf
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.
$ 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
我是一名优秀的程序员,十分优秀!