awk 使 FS 生效

awk to take FS into effect

为什么会出现以下情况?我如何理解其中的逻辑?

$ echo "123456" | awk 'BEGIN {FS="4"; OFS="-"}; {print}'
123456

但是如果我"modify"一些字段,一切都OK:

$ echo "123456" | awk 'BEGIN {FS="4"; OFS="-"}; {=;print}'
123-56

输出字段分隔符只有在以某种方式触及记录后才会生效。来自 GNU AWK manual:

It is important to remember that [=10=] is the full record, exactly as it was read from the input. This includes any leading or trailing whitespace, and the exact whitespace (or other characters) that separates the fields.

It is a common error to try to change the field separators in a record simply by setting FS and OFS, and then expecting a plain print or print [=12=] to print the modified record.

But this does not work, because nothing was done to change the record itself. Instead, you must force the record to be rebuilt, typically with a statement such as =