I am new to Awk, and am having trouble despite this being a common and seemingly simple problem.
I am trying to get the average of a column, but my addition seems to be not working. My script:
BEGIN {FS = ","}
{
AgentDC1 = $4;
AgentDC2 = $5;
AutoDC1 = $23;
AutoDC2 = $24;
CallDuration = $28;
CallDurationMinutes = $27;
CallStart = $33;
ConnectTime = $35;
num = (CallDuration ? CallDuration : 0)
print num
sum += num;
}
END {print sum;}
When run, it prints the values (in quotes, it this normal?) but then prints the average as 0 (without quotes). For example:
$ awk -f search.awk callrecords.csv
"644.0"
"149.0"
"397.0"
...
""
"117.0"
"165.0"
""
0
So empty slots are being printed as ""
, and nothing is being added to sum. I hate to post HOW DO questions, but I am really stuck here, none of the other SOs I found were illuminating.