if (conditional-expression)
action
if (conditional-expression)
{
action1;
action2;
}
if (conditional-expression)
action1
else
action2
if(conditional-expression1)
action1;
else if(conditional-expression2)
action2;
else if(conditional-expression3)
action3;
.
.
else
action n;
$ cat if.txt
beyes 10000 90 98
admin 10001 99 64 72
tony 10002 45 32
tom 10003 12 34 87
lilei 10004 25 70
$ awk '{
if ($3 == "" || $4 == "" || $5 == "")
print "missing score student is", $1;}' if.txt
missing score student is beyes
missing score student is tony
missing score student is lilei
$ awk '{
> if ($3 >= 60 && $4 >= 60 && $5 >= 60)
> print $0, "=>", "Pass";
> else
> print $0, "=>", "Fail";
> }' if.txt
beyes 10000 90 98 => Fail
admin 10001 99 64 72 => Pass
tony 10002 45 32 => Fail
tom 10003 12 34 87 => Fail
lilei 10004 25 70 => Fail
{
total = $3 + $4 + $5;
avg = total / 3;
if (avg >= 60) grade = "A";
else if (avg >= 40) grade = "B";
else if (avg >= 20) grade = "C";
else grade = "D";
print $0, "=>", grade;
}
$ awk -f grade.awk if.txt
beyes 10000 90 98 => A
admin 10001 99 64 72 => A
tony 10002 45 32 => C
tom 10003 12 34 87 => B
lilei 10004 25 70 => C
$ awk '{ORS = NR%3 ? "," : "\n";} {print $0;} END {print "\n";}' if.txt
beyes 10000 90 98,admin 10001 99 64 72,tony 10002 45 32
tom 10003 12 34 87,lilei 10004 25 70,
$ awk 'ORS = NR%3 ? "," : "\n"' if.txt
beyes 10000 90 98,admin 10001 99 64 72,tony 10002 45 32
tom 10003 12 34 87,lilei 10004 25 70,
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |