bash - Gawk-ing an average always returns 0 -


i new awk, , having trouble despite being common , seemingly simple problem.

i trying average of column, addition seems not working. 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, prints values (in quotes, normal?) prints average 0 (without quotes). example:

$ awk -f search.awk callrecords.csv "644.0" "149.0" "397.0" ... "" "117.0" "165.0" "" 0 

so empty slots being printed "", , nothing being added sum. hate post how questions, stuck here, none of other sos found illuminating.

i suppose quotes present in data file. awk not remove them magically.

in awk, when use variable though number, awk ignores characters in variable, starting first 1 can't part og number. if nothing left of variable's value, awk uses value 0.

assuming fields contain quotation marks, value of num start quote, using number result in value 0. still prints out ok, because printed string.


here gawk solution can deal fields contain commas. fpat regex modified gawk manual, while function fix adapted code on same page. both assume "normal" csv convention quotes in quoted fields doubled. (as @edmorton points out in comment, embedded newlines not handled correctly.)

function fix(x) {     if (substr(x, 1, 1) == "\"")       return gensub(/""/, "\"", "g",                     substr(x, 2, length(x) - 2))     else       return x } begin {     fpat = "([^,\"][^,]*|(\"[^\"]*\")+)? } {     agentdc1 = fix($4)     agentdc2 = fix($5)     autodc1 = fix($23)     autodc2 = fix($24)     callduration = fix($28)     calldurationminutes = fix($27)     callstart = fix($33)     connecttime = fix($35)     # unlike original, casts num number.     # it's unnecessary. sum += callduration; fine.     num = callduration+0     print num     sum += num } end {print sum+0} 

Comments