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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Problem with getting awk to multiply a field by a value set based on condition of another field
# 8  
Old 02-02-2020
Wow, thanks. I did not know how that worked. So basically, it works like CSS sort of, as long as I mention the file name at the end, it will look in those files for the input?

Just want to be sure I understand fully.

--- Post updated at 09:46 PM ---

Quote:
Originally Posted by RudiC
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 :
Code:
awk '
FNR == NR       {WEIGHT[$1] = $3
                 next
                }
FNR == 1        {next
                }
                {SUM[$1] += $4 * WEIGHT[$2]
                }
END             {for (s in SUM) print s, SUM[s]
                }
' OFS="\t" file1 file2
Sam     349.45
Chelsey 536.95
Andrew  402.6
Shane   427.75
Ava     434.5

Be aware that nothing is known on the grade calculation algorithm nor the "possible total" that might be needed to calculate it.
I kind of figured it out from looking again but am still confused on a couple of things:

the first line is basically telling the script to get input from first file, creating and storing the array, weight[] which holds the values as such: weight[Homework:0.10, Lab :0.30, Quiz:0.40, Final :0.15, Survey:0.05]

How does it/why does it become weight[$2] though?
# 9  
Old 02-02-2020
No, it stores it like
Code:
awk 'FNR==NR {WEIGHT[$1] = $3; next} END {for (w in WEIGHT) print "WEIGHT[\"" w "\"] =", WEIGHT[w]}' file1
WEIGHT["Lab"] = 0.30
WEIGHT["Homework"] = 0.10
WEIGHT["Survey"] = 0.05
WEIGHT["Quiz"] = 0.40
WEIGHT["Final"] = 0.15

You use each line's $2 immediately as index in the WEIGHT array.
# 10  
Old 02-02-2020
Ok, I think i have the basics of how to use the two files then. So, using the snippet of code you posted gives the total points for each student, but how do I divide those by 575.25 to get the final percentile score for each student?

I have tried a few things and none seem to work. have tried to do SUM[$1]/575.25... Illegal array reference error
have tried to say x=SUM[s]/575.25 at end, but just got 0 all the way down the list and saying x=SUM[s]/575.25 returned a single name and a 0...
# 11  
Old 02-03-2020
Show what you did. Include context. And how you came to 575.25.
# 12  
Old 02-03-2020
I got 575.25 outside of the script. There are 7 Homework assignments, 7 Labs, 7 Quizzes and 1 Final with a total of 100 points possible on each. Then there is the survey worth a possible 5 points.

Knowing the weighted values:
Homework 10%
Labs 30%
Quizzes 40%
Final 15%
Survey 5%

I applied that to the possible total scores, as well
Homework --> 700*0.1 = 70
Labs --> 700 * 0.3 = 210
Quizzes --> 700 * 0.4 = 280
Final --> 100 * 0.15 = 15
Survey --> 5 * 0.05 = 0.25
all weighted percentage values equal 100, so it is good there. so I added the totals 70+210+280+15+.25 = 575.25

I was able to, using the code snippet you provided, divide the totals by the 575.25 and get the final weighted percentile score for each student. I then spent the next several hours trying to get a letter grade assigned to each one based on the percentile score, but had no luck. It currently gives everyone an 'A' no matter what their percent score was, which I am sure a student would like, but not going to work for me...lol.

Very frustrating, trying to learn awk on the fly like this. ... Here is the most current version of the code I have with various comments on why/what is going on:

Code:
awk -F, '
FNR == NR       {WEIGHT[$1] = $3
                 next
                }
FNR == 1        {next
                }
     {SUM[$1] += $4 * WEIGHT[$2]
     per[$1]=SUM[$1]/575.25*100       # gives me the correct weighted final scores for each student
     TOTAL=SUM[$1]/575.25*100       # Assign value to variable cuz I have tried and failed to access it directly from per
     grd[$1]                                            # initializing a new array.. i do not know why. I am just trying things at this point
for (g in grd)                                      # tried without a loop, so now trying in a loop
    if(TOTAL > 97)
    {
       gr="A+"        
    }
    else if (94 < TOTAL <= 97)
    {
       gr="A"                                          # Everyone gets an A which is strange since it is not the first possibility and the student that 
    }	                                                     # actually has an A is not the first record being processed
    else if (90 < TOTAL <= 94)
    {
       gr="A-"        
    }
    else if(87 < TOTAL <= 90)
    {
       gr="B+"        
    }
    else if (84 < TOTAL <= 87)
    {
       gr="B"        
    }
    else if (80 < TOTAL <= 84)
    {
       gr="B-"        
    }
    else if (76 < TOTAL <= 80)
    {
       gr="C+"        
    }
    else if (70 < TOTAL <= 76)
    {
       gr="C"        
    }
    else if (60 < TOTAL <= 70)
    {
       gr="D"        
    }
    else
    {
       gr="E"        
    }
grd[$1]=gr                                          # trying to assign the gr variable to the value of new array... did not work too well
}

END {print "Name\tPercent\tGrade\n" 
           for (p in per)
           printf "%s\t%.2f\t%s\n", p, per[p], grd[$1] }' OFS="\t" file1 data.csv     # have tried grd[p], just the variable gr and doing the calculations inline.

Adding output and noticed that the student I said deserves an A actually deserves an A-....
Output:
Name Percent Grade
Sam 60.75 A
Chelsey 93.34 A
Andrew 69.99 A
Shane 74.36 A
Ava 75.53 A
# 13  
Old 02-03-2020
Try instead
Code:
awk '
function GRD(AVG, R)    {if             (AVG >= 97)     return "A+"
                           else if      (AVG >= 94)     return "A"
                           else if      (AVG >= 90)     return "A-"
                           else if      (AVG >= 87)     return "B+"
                           else if      (AVG >= 84)     return "B"
                           else if      (AVG >= 80)     return "B-"
                           else if      (AVG >= 76)     return "C+"
                           else if      (AVG >= 70)     return "C"
                           else if      (AVG >= 60)     return "D"
                           else                         return "E"
                        }


FNR==NR         {WEIGHT[$1] = $3
                 next
                }
FNR == 1        {next
                }
                {SUM[$1] += $4 * WEIGHT[$2]
                }
END             {for (s in SUM) {AVG = SUM[s]/5.7525
                                 print s, SUM[s], AVG, GRD(AVG)
                                }
                }
' OFS="\t" file1 file2
Sam     349.45  60.7475 D
Chelsey 536.95  93.342  A-
Andrew  402.6   69.987  D
Shane   427.75  74.359  C
Ava     434.5   75.5324 C

There are more elegant grade determination approaches, that is...

Last edited by RudiC; 02-03-2020 at 11:12 AM..
# 14  
Old 02-03-2020
Wow, very simple. But why wouldn't it work the way I was trying? (Using an if/else block)
What does the 'R' represent in the second arg for the function?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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. (17 Replies)
Discussion started by: CatchMe
17 Replies

2. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: cmccabe
2 Replies

6. Shell Programming and Scripting

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}'... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: jghi123
2 Replies

8. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: kichu
1 Replies

9. UNIX for Dummies Questions & Answers

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... (7 Replies)
Discussion started by: treesloth
7 Replies

10. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: FrancoisCN
1 Replies
Login or Register to Ask a Question