Awk application-calculate the average score of Students
Let's take a look at an example of the awk application. First, we add the scores of a series of students and calculate their average values. The specific data of the input file is as follows:
$ Cat grades
John 85 92 78 94 88
Andrea 89 90 75 90 86
Jasper 84 88 80 92 84
The student's name is followed by five scores. The following script shows the average score of each student:
$ Cat grades. awk
# Calculate the average value of five scores
{Total = $2 + $3 + $4 + $5 + $6
Avg = total/5
Print $1, avg}
This script adds the second to sixth fields to obtain the sum of the five scores. Divide the total value by 5 and assign the result to the variable avg. Print statement Print the Student name and average score. Note that we can save the avg value assignment and use the average score as part of the print statement, as shown below:
Print $1, total/5
This program makes it easy to write programs in awk. Awk parses the input into fields and records. You do not need to read individual characters or declare data types. Awk will automatically do this for you.