Given a input file with record format... (ID #, First name, Last name, Score1, Score2, Score3, Score(N).. Important note: There will be numerous records in file (not just 1 row).
I would like to be able to provide output format such as.. (ID #, Average Score, Grade letter corresponding to average score)
Example of input:
900123123 Joe Brown 100 90 80
900900900 Tyler Black 80 95 75
900231231 Stephen Williams 70 75 80
900111111 Andrew Smith 85 75 90
Example of output:
900123123 90 A
900900900 83.3 B
900231231 75 C
900111111 83.3 B
My issue is the if-statements to determine what grade letter to assign. Here is my current code:
#!/bin/bash
awk '
BEGIN {FS=OFS=" "}
{
sum=0; average=0
for(i=3;i<=NF;i++)
{sum+=$i;}
average = (sum/(NF-3))
if (average -ge 90)
print $1, $2, $3, average, " A";
else if(average -ge 80 && -le 90)
print $1, $2, $3, average, " B";
else if(average -ge 70 && -le 80)
print $1, $2, $3, average, " C";
else if(average -ge 60 && -le 70)
print $1, $2, $3, average, " D";
else
print $1, $2, $3, average, "F";
}' grades.txt
This will result in the output:
900123123 Joe Brown 90 A
900323323 Tyler Black 83.3333 A
900231231 Stephen Williams 75 A
900232232 Andrew Smith 83.3333 A
0 A
Why is the first if statement being hit every time even when average is less than 90? We know this because it is used inside of the print statement and prints out A with any number.
Also, I have no idea why 0 A is output and what the cause of this may be.