Layla_K
Layla_K

Reputation: 75

multiple statements after if statement

I have an awk line to calculate an average that works fine but when I put it into an if statement, I get a syntax error referring to the part with "END". I want to calculate the average only if certain conditions are fulfilled.

Line for calculating average that works:

awk '{ sum += $2; n++ } END { if (n > 0) print sum / n; }' input.txt

Line for calculating average after if statement which doesn't work:

awk '{if ( $1 > 5 ) { {sum += $2; n++} END { if (n > 0) print sum / n; }}}' input.txt

I would like to know where the error is, changing the type and number of brackets did not help.

Upvotes: 0

Views: 279

Answers (1)

karakfa
karakfa

Reputation: 67467

try this

awk '$1>5 {sum+=$2; n++}
     END  {if(n) print sum/n}' file

Upvotes: 2

Related Questions