Question
AWK Programming - Please show new working code with screenshot please. Save the following data in a file named teamlist.txt: Name,Team,First Test, Second Test, Third
AWK Programming - Please show new working code with screenshot please.
Save the following data in a file named teamlist.txt:
Name,Team,First Test, Second Test, Third Test Tom,Red,5,17,22 Joe,Green,3,14,22 Maria,Blue,6,18,21
Fred,Blue,2,15,23 Carlos,Red,-1,15,24 Phuong,Green,7,19,21 Enrique,Green,3,16,20 Nancy,Red,9,12,24
Write an AWK script that will compute the average score for every person in the list, the average score for each test, and the average score for each team. If a score is negative, that means the person missed the test, and the score must not become part of the average.
Print the output to look like the following. In the list by name, the names must be left justified in a field of size 10 (hint: %-10s in printf), and the averages must be seven characters wide with two digits to the right of the decimal point (%7.2f).
Name -------- Tom Joe Maria Fred Carlos Phuong 15.67 Enrique 13.00 Nancy 15.00 --------------------------------- Average for Test 1: 5 Average for Test 2: 15.75 Average for Test 3: 22.125 --------------------------------- Average for Red Team: 16 Average for Green Team: 13.8889 Average for Blue Team: 14.1667
Average ------- 14.67 13.00 15.00 13.33 19.50
1
AWK Programming
The Pseudocode
Below is a pseudo-code to help you write the program.
BEGIN{
set FS to a comma
print the header
}
{ if (NR > 1) # this test skips the first line of the file {
Set individualTotal and individualCount to zero
for (field=3; field <=5; field++) {
if ($field >= 0) {
add contents of $field to individualTotal add one to individualCount
# Now update the testTotal and testCount arrays # subtract 2 from the field number so that our arrays begin at 1 add contents of $field to testTotal indexed by (field-2) add 1 to testCount array indexed by (field-2)
# Use contents field 2 as the index for the team array
add contents of $field to teamTotal array indexed by $2
add 1 to teamCount array indexed by $2
} }
print the person's name and his individual average
} }
END { print "------------------" for (n=1; n<=3; n++) {
print average for test n using testTotal and testCount arrays }
print "------------------"
print average for "Red" team print average for "Green" team print average for "Blue" team (these all use the teamTotal and teamCount arrays)
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started