Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts

May 20, 2011

awk after the number of field or the value of field changed

# from awk manpage
References to non-existent fields (i.e. fields after $NF) produce the null-string.
However, assigning to a  non-exis‐tent field (e.g., $(NF+2) = 5) increases the value of NF,
creates any intervening fields with the null string as their value, and causes the value
of $0 to be recomputed, with the fields being separated by the value of  OFS.

References to negative numbered fields cause a fatal error.

Decrementing NF causes the values of fields past the new value to be lost,
and the value of $0 to be recomputed, with the fields being separated by the value of OFS.

Assigning a value to an existing field causes the whole record to  be  rebuilt  when  $0  is  referenced.
Similarly, assigning a value to $0 causes the record to be resplit, creating new values for the fields.


意思是:
1. 引用不存在的字段,会使增加NF的值,$0要根据OFS的值重新创建,中间的字段设置为空字符串, 然后根据FS的值重新对$0进行分割.
如:
$ echo 'a b c' | awk '{print $0; $6=60; OFS=":"; print $0}'
a b c
a:b:c:::60
可以清楚的看到, 第4,5字段的值都是空字符串.

2. 引用编号小于零的字段,会引起错误.
如:
$ cal | awk '{print $-1}'
awk: (FILENAME=- FNR=1) fatal: attempt to access field -1

3. 减少NF的值,编号大于NF的字段将丢失,$0根据OFS的值重新创建,然后根据FS的值重新分割记录.
如:
$ echo 'a b c d e f' | awk '{print $0; NF=3; print $0}'
a b c d e f
a b c

4. 给存在的字段赋值,使$0根据OFS的值重新创建,然后根据FS的值重新分割记录.
如:
$ echo 'a b c' | awk '{print $0; $1=$1;OFS=":"; FS=":"; print $0; print $1}'
a b c
a:b:c
a

5. 给$0赋值,$0现在是被赋予的新值,$0根据FS的值重新分割记录.
$ echo 'a b c' | awk '{print $0; $0=$0;OFS=":"; FS=":"; print $0; print $1}'
a b c
a b c
a

echo 'a b c' | awk '{print $0; $0="c d e"; print $0; print $1}'
a b c
c d e
c

note:
  a. 4, 5是ChinaUnix.net Shell 编程大赛的第8题.
  b. 可以发现当字段个数或者字段被修改后,都要根据OFS的值进行$0重建, 然后,$0根据FS的值重新分割.
  c. 只修改$0, 使$0用新值根据FS的值重新分割.