Hi, I am trying to get my sorting script to work, and add to it.
Here is my code so far:

#/bin/sh
echo "Name   Exam1   Exam2   Exam3  Total  Grade" > final.txt
awk -f 9.awk grades.txt | sort +4 -5 >> final.txt

and then 9.awk is:

#BEGIN {printf "Name\tExam1\tExam2\tExam3\tTotal\tGrade";}
{
        if($2+$3+$4>90) {printf "%s\t%d\tA\n",$0,$4+$2+$3;}
        else
                if(($2+$3+$4>=80)&&($2+$3+$4<90)) {printf "%s\t%d\tB\n",$0,$2+$3+$4;}
                else
                if(($2+$3+$4>=70)&&($2+$3+$4<80)) {printf "%s\t%d\tC\n",$0,$2+$3+$4;}
                        else
                if(($2+$3+$4>=50)&&($2+$3+$4<70)) {printf "%s\t%d\tD\n",$0,$2+$3+$4;}
                  else {printf "%s\t%d\tF\n",$0,$2+$3+$4;}
}

grades.txt contains the following:

Name	Exam1	Exam2	Exam3
Tom	23	12	30
Jack	30	35	38
Jane	31	25	19
Brian	38	38	40
Lisa	30	28	32
Nick	33	30	15
Karen	25	28	12

what I want it to do at the moment is add 3 more columns and look like this:

Name Exam1 Exam2 Exam3 Total Average Grade
Brian   38   38   40   116   96   A
Jack    30   35   38   103   85   B
Lisa    30   28   32   90    75   C
Nick    33   30   15   78    65   C
Jane    31   25   19   75    62   D
Tom     23   12   30   65    54   D
Karen   25   28   12   65    54   D

However what I currently have is this:

Name   Exam1   Exam2   Exam3  Total  Average  Grade
Name	Exam1	Exam2	Exam3	0	F
Jack	30	35	38	103	A
Brian	38	38	40	116	A
Karen	25	28	12	65	D
Tom	23	12	30	65	D
Jane	31	25	19	75	C
Nick	33	30	15	78	C
Lisa	30	28	32	90	F

right now it is sorting on the total column. which is wrong,I know I need to have it sort on the average column, however I cant get it to display an average. Also I have the extra line of Name Exam1.. etc that I cant get rid of.

Any help would be much appreciated.

All I really need to know is how to have the script calculate the average.

This article has been dead for over six months. Start a new discussion instead.