## Problem with getting awk to multiply a field by a value set based on condition of another field

 awk, field, newbies, problem, solved Problem with getting awk to multiply a field by a value set based on condition of another field
Problem with getting awk to multiply a field by a value set based on condition of another field

Hi,

So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple students using all assignments as input. (Sample below)

The input file is a csv. I took all of the assignment names from \$3 and calculated the highest grade, lowest grade and average grade for each one. the code I used to do this is:

That gave me the desired out put. Using the same input file, I want to now group by the Student name and then give weight to each assignment grade.

Homework = 0.10
Lab = 0.30
Quiz = 0.40
Final = 0.15
Survey= 0.05

so the logic is this: If \$2 = "Homework" then \$4 = \$4*0.10 and so on for each assignment Category. The all I need to do is sum all of the \$4 for each student individually. On top of this, I also need to assign a letter grade based on the value of each student's total class grade, so I am not sure even how to proceed there at all.

However, I cannot make this happen and return all kinds of goofy results, including somehow printing the output headers several times with nothing else and they are set before anything else and are no where near a loop. So, I am totally confused. Here is the most recent failure:

So, if anyone can help me out here, I would appreciate it.

The Desired output is:

INPUT FILE:
data.csv
 cotilloe View Public Profile for cotilloe Find all posts by cotilloe
Hi some quick thought on the last code snippet:
In awk the middle section is processed per line, so you should leave out:
for(i=1;i<=NR;i++)
The results should be stored in arrays so they can be used in the END section.
The END section contains code after all lines have been read in the middle section,
so the following has no business there:
 Scrutinizer View Public Profile for Scrutinizer Find all posts by Scrutinizer
Ahh!! Thanks for that info. It explains why I get the repeated output headers then. Also, I did not realize that about the END statement. I understood it as you do not perform anything in BEGIN, but never knew that calcs and stuff should be done before END.

My biggest issue is that I am not sure of how to take an associative array and have another array stored within it. Basically I need to have it be Student_Name[Assignment_Category{Assignment Scores] where it would look like this:

Steve
Lab --- Homework ----- Quiz ---- Final ----Survey
44 ---------- 98 ---------- 78 -------- 88 ------- 5
66 --------- 100 ---------- 85
77 ---------- 88 ---------- 92
86 ---------- 77 ---------- 77

So then I have all of Steve's assignments and their grades, then i can multiply each of the grades by the appropriate weight
 cotilloe View Public Profile for cotilloe Find all posts by cotilloe
Hi
I honestly didn’t understand anything except the first part of the program.
So I’ll just tweak the style to make it better to read

Last edited by nezabudka; 02-02-2020 at 04:56 PM..
 nezabudka View Public Profile for nezabudka Find all posts by nezabudka
How far would this get you, printing the weighted total for each student. The weight per category is delivered in file1 in the form you posted :
Be aware that nothing is known on the grade calculation algorithm nor the "possible total" that might be needed to calculate it.

Last edited by RudiC; 02-02-2020 at 04:22 PM..
This User Gave Thanks to RudiC For This Post:
 RudiC View Public Profile for RudiC Find all posts by RudiC
That actually helps alot, as I had tried something like that. What I tried to do was:

That did not work, but by storing the weights in in an array, I see how it could be workable. My only question is how to store/use multiple values.
If \$2 = Homework then \$4 needs to be multiplied by 0.1
if \$2 = Quiz then \$4 needs to be multiplied by 0.4
if \$2 = Lab then \$4 needs to be multiplied by 0.3
if \$2 = Final then \$4 needs to be multiplied by 0.15
if \$2 = Survey then \$4 needs to be multiplied by 0.05

Then, all I would need to do is some \$4 with the new values and have the overall percent for each student.
 cotilloe View Public Profile for cotilloe Find all posts by cotilloe
Quote:
Originally Posted by cotilloe
That actually helps alot, as I had tried something like that. What I tried to do was:

That did not work, but by storing the weights in in an array, I see how it could be workable. My only question is how to store/use multiple values.
If \$2 = Homework then \$4 needs to be multiplied by 0.1
if \$2 = Quiz then \$4 needs to be multiplied by 0.4
if \$2 = Lab then \$4 needs to be multiplied by 0.3
if \$2 = Final then \$4 needs to be multiplied by 0.15
if \$2 = Survey then \$4 needs to be multiplied by 0.05

Then, all I would need to do is some \$4 with the new values and have the overall percent for each student.

The weight per category is delivered in file1, in the shape you showed post #1:
 RudiC View Public Profile for RudiC Find all posts by RudiC

Test Your Knowledge in Computers #392
Difficulty: Medium
The Linux Standard Base was formed in 2001 as an attempt to standardize the internal structures of Linux-based systems for increased compatibility.
True or False?

## Change the field color based on condition in email

Request your help to change the field color based on condition , if it is otherthan 0. using html in unix. Here is my condition for(i=1;i<=NF;i++) { print "<td> "\$i"</td> } Please use CODE tags when displaying sample input, output, and code segments.

## awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of \$6 there may be multiple values seperated by a ;, to \$8, if \$8 is . (lines 1 and 3 are examples). If that condition \$8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output...

## Perl to update field based on a specific set of rules

In the perl below, which does execute, I am having trouble with the else in Rule 3. The digit in f{8} is extracted and used to update f accordinly along with the value in f. There can be either - * or + before the number that is extracted but the same logic applies, that is if the value is greater...

## awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If \$4 matches in each line then the first matching sequential \$6 value is added to \$2, unless the value is 1, then the original \$2 is used (like in the case of line...

## awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in \$2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in \$2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I...

## awk to count condition in field

I am trying to confirm the counts from another code and tried the below awk, but the syntax is incorrect. Basically, outputting the counts of each condition in \$8. Thank you :) awk '\$8==/TYPE=snp/ /TYPE=ins/ /TYPE=del/ {count++} END{print count}'...

## Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the \$98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if (\$108=="T")sub(\$98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.\$/ sub(\$98,"T")} { print}' test.txt but...

## Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20...

## awk - Summing a field based on another field

So, I need to do some summing. I have an Apache log file with the following as a typical line: 127.0.0.1 - frank "GET /apache_pb.gif HTTP/1.0" 200 2326 Now, what I'd like to do is a per-minute sum. So, I can have awk tell me the individual minutes, preserving the dates(since this is a...

## Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50...