Awk application-calculate the average score of Students

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.