- 论坛徽章:
- 8
|
原帖由 ruifox 于 2007-7-23 09:40 发表 ![]()
这个问题颇有点古怪,FS的赋值方式明显不对,但执行的结果也很难理解!
为什么第一行能够执行正确,但第二行数据就出现问题,而且似乎还影响了OFS,奇怪!
期待高手!
awk 'FS="\t" gsub(/a/,"A",$1)'执行 ...
第一行FS="\t"以前域已经拆分好了
第二行行才按\t拆分,结果是非常相当的正常的
- Advanced Notes: Changing FS Does Not Affect the Fields
- According to the POSIX standard, awk is supposed to behave as if each record is split into fields at the time it is read. In particular, this means that if you change the value of FS after a record is read, the value of the fields (i.e., how they were split) should reflect the old value of FS, not the new one.
- However, many implementations of awk do not work this way. Instead, they defer splitting the fields until a field is actually referenced. The fields are split using the current value of FS! (d.c.) This behavior can be difficult to diagnose. The following example illustrates the difference between the two methods. (The sed18 command prints just the first line of /etc/passwd.)
- sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'
- which usually prints:
- root
- on an incorrect implementation of awk, while gawk prints something like:
- root:nSijPlPhZZwgE:0:0:Root:/:
复制代码 |
|