Let's look at an example of an awk application that adds a series of students ' scores and then calculates their averages. Here is the specific data for the input file:
$CatGrades
John - the +94 the
Andrea the - the - the
Jasper - the the the -
There are 5 grades behind the student's name. The following script will give the average score for each student:
$CatGrades.awk
#求5个成绩的平均值
{Total= $+ $+$4 + $+$6
Avg=Total/5
Print $,Avg}
The script adds the second to sixth fields together to get the sum of 5 scores. Divide the value of total by 5 and assign the result to the variable avg. Print statement Prints the student's name and average score. Note that we can save. Avg assigns and calculates the average score as part of the print statement, as follows:
Print $,Total/5
This program gives us an idea of how easy it is to write programs in awk. Awk parses the input into fields and records. You don't have to read individual characters and declare data types. Awk will automatically do the work for you.
Reference: http://www.linuxawk.com/communication/486.html
awk application-Calculate student's average score