Note: this issue only applies to Solaris's default awk.
GNU awk/gawk and Solaris's XPG4 awk do not exemplify this behavior.
Furthermore, this is more of a "why" question, not a "how do I fix this" question.
Using the following example input:
a 10
b 20
c 42
d 99
f 01
In my awk script, I'm extracting the last field $NF
to use as a value and using everything else as a key. To do this, I set $NF = ""
and use what remains in $0
as the key.
However, I get different results depending on if I use $0
directly or if I assign it to a variable first:
awk '{
# Extracting and removing the value
value = $NF;
$NF = "";
# Copying $0 to a variable
key = $0;
# Displaying the difference between "key" and "$0"
printf("key = <%s>\n", key);
printf("$0 = <%s>\n", $0 );
printf("\n");
}' < input.txt
This results in the following output:
key = <a 10>
$0 = <a >
key = <b 20>
$0 = <b >
key = <c 42>
$0 = <c >
key = <d 99>
$0 = <d >
key = <f 01>
$0 = <f >
Note that $0
is missing $NF
, but key
somehow still contains it even though it should be a direct copy of $0
.
Why does this happen?
Is there something in the formal awk language definition that defines this behavior?