Tree55Topz
Tree55Topz

Reputation: 1142

Bash(awk) - Getting averages from rows of numbers

I am to do a bash program where it reads a name, stores it, then reads scores to find if an avg is above 60. If it is, print out their name. So the file looks like this

tim 50 60 70
roy 90 80 90

The program should only print out "roy" because he was the only one with a passing grade. This is what I got. I am just unsure about where to put things like ;,{},and() so I'm sure my code is hideous:

#!/bin/bash
awk '{name=$i}
{avg=0}
{
  count=0;
  for(i=2;i<=NF;i++)
    avg+=$i
  count++;
}
avg=avg/count {
  if(avg >= 60) 
    printf("%s ", name)
}'

Suprisingly, it compiles with no errors, however nothing prints.

Upvotes: 2

Views: 68

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207445

You were nearly there:

awk '{
  name=$1
  tot=0
  for(i=2;i<=NF;i++)
    tot+=$i
  if(tot/(NF-1)>60)
    print name
  }' yourfile

Upvotes: 3

Related Questions