3

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?

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • 1
    It looks like that version doesn't rebuild `$0` when you change the last field. `$1 = $1` would probably trigger it; see https://www.gnu.org/software/gawk/manual/html_node/Changing-Fields.html – Benjamin W. May 25 '16 at 17:22
  • This being said, according to the awk as specified by POSIX (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html), setting any field should cause re-evaluation of `$0`. – Benjamin W. May 25 '16 at 17:25
  • 1
    @BenjaminW. - I think you're on to something. If I change the order to `print $0`, `key = $0`, `print key`, then the output is identical. I'm not sure why `print $0` forces an update but `key = $0` doesn't, but at least we're getting somewhere. – Mr. Llama May 25 '16 at 17:31
  • 4
    realize that Sun `awk` is the original awk version created by Aho, Wein.., Kern.. Any Sun user knew to use `nawk` to get the features as documented in 'The AWK Programming Language' (Wiley pub). There are occasional discussions in that book about the original awk vs the "New" awk (1988~) ;-) . But you're trying to count the number of angels dancing on the head of a pin Move on, time flies ;-) Good luck – shellter May 25 '16 at 17:33
  • 1
    @shellter -berger -ighan – tripleee May 25 '16 at 17:39
  • 1
    @tripleee ; Yep thanks. I wasn't sure if my answer would exceed the limit, so I cheated to save some space AND I'm never quite sure (with out digging out the book), if it's Weinberg or Weinberger, or Wien.. . – shellter May 25 '16 at 17:43
  • 1
    With respect to *why*, what's likely very close to the original Solaris `awk` source code can be found at http://src.illumos.org/source/xref/illumos-gate/usr/src/cmd/awk/ – Andrew Henle May 25 '16 at 19:36
  • instead of another $1 = $1, try changing :::::::::: value = $NF ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: to value = $(NF+=0) – RARE Kpop Manifesto May 29 '22 at 10:09

0 Answers0