Can someone help me getting average of a column using awk with condition on other column -


awk -f, '{if ($2 == 0) awk '{ total += $3; count++ } end { print total/count }' cln_tapes_lon; }' /tmp/cln_tapes_lon  awk: {if ($2 == 0) awk { awk:                   ^ syntax error bash: count++: command not found 

just fun, let's @ what's wrong original version , transform works, step step. here's initial version (i'll call version 0):

awk -f, '{if ($2 == 0) awk '{ total += $3; count++ } end { print total/count }' cln_tapes_lon; }' /tmp/cln_tapes_lon 

the -f, sets field separator comma character, later comment seems indicate columns (fields) separated spaces. let's rid of it; whitespace-separation awk expects default. version 1:

awk '{if ($2 == 0) awk '{ total += $3; count++ } end { print total/count }' cln_tapes_lon; }' /tmp/cln_tapes_lon 

you seem attempting nest call awk inside awk program? there's never call that, , wouldn't way anyway. let's rid of mismatched quotes while we're @ it: note in passing cannot nest single quotes inside pair of single quotes way: you'd have escape them somehow. there's no need them @ here. version 2:

awk '{if ($2 == 0) { total += $3; count++ } end { print total/count } }' /tmp/cln_tapes_lon 

this close not quite right: end block executed when lines of input finished processing: doesn't make sense have inside if. let's move outside braces. i'm going tighten whitespace. version 3:

awk '{if ($2==0) {total+=$3; count++}} end{print total/count}' /tmp/cln_tapes_lon 

version 3 works, , stop here. awk has handy way of specifying run block of code against lines match condition: 'condition {code}' yours can more written as:

awk '$2==0 {total+=$3; count++} end{print total/count}' /tmp/cln_tapes_lon 

... which, of course, pretty john1024 suggested.