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}'
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:
NF
or NR
awk
builtin variables)LAST
or xyz
, and see if the execution is the sameThis 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.
Hopefully it's correct
{
# 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.
thx MadeInGermany and Peasant