awk not clear

can anyone tell me the working of this logic.
here

"last" 

is a variable or it has it own meaning . getting confused.

awk '{if (last && $2 != last) {print last, sum; sum=0} 
sum=sum+ $3; last = $2}
 END {print last, sum}'

There can be various ways to escape confusion:

  • read the man page to find out about "reserved words"
  • is the questionable identifier assigned a value, or (meaningfiully!) used without assignment (as e.g. the NF or NR awk builtin variables)
  • use another identifier, e.g. LAST or xyz , and see if the execution is the same

This is the main loop that is run for each input record.
The variable "last" is tested for a value that was assigned in a previous loop cycle.
What happens in the first loop cycle? "last" is undefined, and is casted to a 0 (false) by the (boolean) context.

1 Like

Hopefully it's correct :slight_smile:

{
# On lines that meet condition -> if 'last' has a value and second field value of input is not equal to 'last'
if (last && $2 != last) {
        # print variables 'last' and 'sum', set 'sum' to zero
        print last, sum; sum=0
        }
# On each line 
        # define variable 'sum' with the current value of 'sum' incremented by the value third field of input
        # define variable 'last' as the value of second field.
        sum=sum+ $3; last = $2
}
END {
# print final values of 'last' and 'sum', after entire file is processed.
print last, sum
}

Regards
Peasant.

1 Like

thx MadeInGermany and Peasant