$ cat employee.txt
1 director 2 30000
2 manager 4 20000
3 engineer 8 9000
4 technician 20 4500
$ cat total.awk
BEGIN {
total = 0;
}
{
Emno = $1;
Type = $2;
SalaryAmount = $3 * $4;
total = total + SalaryAmount;
print Emno, " ", Type "\t", SalaryAmount"RMB";
}
END {
print "Total Salary = " total"RMB";
}
$ awk -f total.awk employee.txt
1 director 60000RMB
2 manager 80000RMB
3 engineer 72000RMB
4 technician 90000RMB
Total Salary = 302000RMB
$ cat score.txt
beyes 10000 90 78 98
admin 10001 99 64 72
tony 10002 45 32 56
tom 10003 12 34 87
lilei 10004 48 25 70
$ cat score.awk
BEGIN {
chinese = 0;
math = 0;
english = 0;
print "Name\tID\t Average Score";
}
{
total = $3 + $3 + $5;
chinese = chinese + $3;
math = math + $4;
english = english + $5;
print $1"\t"$2"\t", total/3;
}
END {
print "Average of chinese = " chinese/NR;
print "Average of math = " math/NR;
print "Average of english = " english/NR;
}
$ awk -f score.awk score.txt
Name ID Average Score
beyes 10000 92.6667
admin 10001 90
tony 10002 48.6667
tom 10003 37
lilei 10004 55.3333
Average of chinese = 58.8
Average of math = 46.6
Average of english = 76.6
cat student_htm.awk
BEGIN {
title = "AWK";
print "<html>\n<title>"title"</title><body bgcolor=\"#ffffff\">\n<table border=1><th colspan=2 align=centre>Student Details</th>";
}
{
Name = $1;
ID = $2;
print "<tr><td>"Name"</td><td>"ID"</td></tr>";
}
END {
print "</table></body>\n</html>";
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |