Awk If Statement Examples
if
$ awk '{ if ($3 =="" || $4 == "" || $5 == "") print "score of the student",$1,"is missing";' }'
if else
$ awk '{ if ($3 >=80 && $4 >= 80 && $5 >= 80) print $0,"=>","Pass"; else print $0,"=>","Fail"; }
else if
$ cat calc_grade.awk { total=$3+$4+$5; mean=total/3; if ( mean >= 90 ) grade="A"; else if ( mean >= 80) grade ="B"; else if (mean >= 70) grade ="C"; else grade="D"; print $0,"=>",grade; }
$ awk -f calc_grade.awk student-recort AAA 2111 70 80 75 => C BBB 2123 60 55 40 => D CCC 2212 40 42 => D DDD 2313 88 98 91 => A EEE 2411 30 45 => D