Appending to a file without printing to screen

#!/bin/csh
awk 'BEGIN     { print "Name           Exam1           Exam2           Exam 3        Total        Grade" }' grades | tee gradesorted
awk '{if ($2+$3+$4<50){grade="F"}else if ($2+$3+$4>49 && $2+$3+$4<65)
{grade="D"}else if ($2+$3+$4>64 && $2+$3+$4<80){grade="C"}
else if ($2+$3+$4>79 && $2+$3+$4<90){grade="B"}else{grade="A"}}
{print $0, "          ", $2+$3+$4, "          ", grade;}' grades | tee gradesorted

What I'm trying to do change both the prints so that what's in the quotations gets appended to the file rather than printed to the screen. I've tried using sed with no success. Can anyone point me in the right direction? Thanks.

awk '...stuff...' > gradesorted

Not that it matters in this instance, but see these pages:
Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful

Why two awk programs?

#!/bin/sh

awk 'BEGIN {
   format = "%-15s%-15s%-15s%-15s\n"
   printf format, "Name", "Exam1", "Exam2", "Exam 3", "Total", "Grade"
  }
{
 total = $2 + $3 + $4
      if (total < 50) {grade = "F"}
 else if (total < 65) {grade = "D"}
 else if (total < 80) {grade = "C"}
 else if (total < 90) {grade = "B"}
 else {grade="A"}
}
{printf format, $1, $2, $3, $4, total, grade}' grades > gradesorted

I don't know why I was using two awk's...

Thanks for helping clean up my sloppy code. The only problem I'm having, at this point, is sorting the gradessorted file without the header (Name, Exam1, etc.) being on the bottom. Is there a way to exclude lines from a sort?

Pipe the printf through sort:


awk 'BEGIN {
   format = "%-15s%-15s%-15s%-15s%-15s%-15s\n"
   printf format, "Name", "Exam1", "Exam2", "Exam 3", "Total", "Grade"
  }
{
 total = ( $2 + $3 + $4 ) / 3 # I think you want the average
      if (total < 50) grade = "F"
 else if (total < 65) grade = "D"
 else if (total < 80) grade = "C"
 else if (total < 90) grade = "B"
 else grade="A"

 format = "%-15s%-15s%-15s%-15s%-15d%-15s\n"
 printf format, $1, $2, $3, $4, total, grade | "sort"
} ' grades > gradesorted

Marvellous. Thanks a bunch!