awk application-Calculate student's average score

Source: Internet
Author: User

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

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.