LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Please help me with awk to output list of students, their grade as % and letter grade (https://www.linuxquestions.org/questions/linux-newbie-8/please-help-me-with-awk-to-output-list-of-students-their-grade-as-and-letter-grade-4175672667/)

jessmister04 04-06-2020 06:09 PM

Please help me with awk to output list of students, their grade as % and letter grade
 
New to Unix and this is my first week of using awk. I attached the csv file and I am trying to output the NAME,PERCENT,LETTER for each student after adding up all their assignments scores and / possible points. Please help me if you can, I want to learn and understand the answer. The code I have started is below and I am confused as to if I need only 1 for loop or 2? Also, I am not too familiar with syntax and printing on UNIX. I only have taken one class in python.



BEGIN{

FS = ","

}
$4~/[0-9]/ {

EARN[$1$2]+=$4
POSSIBLE[$1$2]+=$5
STUDENTS[$1]++
CATEGORIES[$2]++
}
END{

# for(ASSIGNMENT in EARN) {
# print ASSIGNMENT,EARN[ASSIGNMENT]/POSSIBLE[ASSIGNMENT]
# }


for (STUDENT in STUDENTS) {
SURVEY=(EARN[$1"Survey"]/POSSIBLE[$1"Survey"])*0.05
HOMEWORK=(EARN[$1"Homework"]/POSSIBLE[$1"Homework"])*0.10
LAB=(EARN[$1"Lab"]/POSSIBLE[$1"Lab"])*0.30
QUIZ=(EARN[$1"Quiz"]/POSSIBLE[$1"Quiz"])*0.40
FINAL=(EARN[$1"Final"]/POSSIBLE[$1"Final"])*0.15
TOTAL=(SURVEY+HOMEWORK+LAB+QUIZ+FINAL)
LETTER="A"
if (TOTAL < 90) {
LETTER="B"
}
if (LETTER < 80) {
LETTER="C"
}
if (LETTER < 70) {
LETTER="D"
}
if (LETTER < 60) {
LETTER="F"
}

for(ASSIGNMENT in EARN) {
print ASSIGNMENT,EARN[ASSIGNMENT]/POSSIBLE[ASSIGNMENT],TOTAL[ASSIGNMENT],LETTER[ASSIGNMENT]

}
}
}

syg00 04-06-2020 06:38 PM

First thing - learn to use [code] tags - else no-one few will bother reading your post(s). I didn't see an attachment, add a few line of input in-line (and in [code] tags). You will no doubt get suggestions for better ways of doing it, but looks pretty good for a learner.
Code:

BEGIN{
 
        FS = ","

}
$4~/[0-9]/ {

        EARN[$1$2]+=$4
        POSSIBLE[$1$2]+=$5
        STUDENTS[$1]++
        CATEGORIES[$2]++
}
END{

#      for(ASSIGNMENT in EARN) {
#              print ASSIGNMENT,EARN[ASSIGNMENT]/POSSIBLE[ASSIGNMENT]
#      }


        for (STUDENT in STUDENTS) {
                SURVEY=(EARN[$1"Survey"]/POSSIBLE[$1"Survey"])*0.05
                HOMEWORK=(EARN[$1"Homework"]/POSSIBLE[$1"Homework"])*0.10
                LAB=(EARN[$1"Lab"]/POSSIBLE[$1"Lab"])*0.30
                QUIZ=(EARN[$1"Quiz"]/POSSIBLE[$1"Quiz"])*0.40
                FINAL=(EARN[$1"Final"]/POSSIBLE[$1"Final"])*0.15
                TOTAL=(SURVEY+HOMEWORK+LAB+QUIZ+FINAL)
                LETTER="A"
                if (TOTAL < 90) {
                        LETTER="B"
                }
                if (LETTER < 80) {
                        LETTER="C"
                }
                if (LETTER < 70) {
                        LETTER="D"
                }
                if (LETTER < 60) {
                        LETTER="F"
                }
       
        for(ASSIGNMENT in EARN) {
              print ASSIGNMENT,EARN[ASSIGNMENT]/POSSIBLE[ASSIGNMENT],TOTAL[ASSIGNMENT],LETTER[ASSIGNMENT]
           
        }
}
}


jessmister04 04-06-2020 06:56 PM

here are a few lines of the input from the csv file. I will code tags and upload soon to help explain what I am doing .

Student,Catehory,Assignment,Score,Possible
Chelsey,Homework,H01,90,100
Chelsey,Homework,H02,89,100
Chelsey,Homework,H03,77,100
Chelsey,Homework,H04,80,100
Chelsey,Homework,H05,82,100
Chelsey,Homework,H06,84,100
Chelsey,Homework,H07,86,100
Chelsey,Lab,L01,91,100
Chelsey,Lab,L02,100,100
Chelsey,Lab,L03,100,100
Chelsey,Lab,L04,100,100
Chelsey,Lab,L05,96,100
Chelsey,Lab,L06,80,100
Chelsey,Lab,L07,81,100
Chelsey,Quiz,Q01,100,100
Chelsey,Quiz,Q02,100,100
Chelsey,Quiz,Q03,98,100
Chelsey,Quiz,Q04,93,100
Chelsey,Quiz,Q05,99,100
Chelsey,Quiz,Q06,88,100
Chelsey,Quiz,Q07,100,100
Chelsey,Final,FINAL,82,100
Chelsey,Survey,WS,5,5
Sam,Homework,H01,19,100
Sam,Homework,H02,82,100
Sam,Homework,H03,95,100
Sam,Homework,H04,46,100
Sam,Homework,H05,82,100
Sam,Homework,H06,97,100
Sam,Homework,H07,52,100
Sam,Lab,L01,41,100

jessmister04 04-06-2020 07:17 PM

Last login: Mon Apr 6 16:53:33 on ttys000
Jesses-MacBook:~ jessejansen$ ls
Applications Library ifnumberic.sh
Desktop Movies ifnumeric.sh
Documents Music io.sh
Downloads Pictures myfirstscript.sh
Lab2Part1.sh Public teledir.sh
Lab2Part2.sh ifcommand.sh teledir.txt
Jesses-MacBook:~ jessejansen$ cd Desktop
Jesses-MacBook:Desktop jessejansen$ cd Lab3
Jesses-MacBook:Lab3 jessejansen$ ls
03LabData.csv Lab3Part1.awk data.awk data1.awk
Jesses-MacBook:Lab3 jessejansen$ vim data1.awk


Hopefully this helps, please let me know:)


BEGIN{ #Begin block statement, this runs before the script

FS = "," #field separator set to comma

}
$4~/[0-9]/ {

EARN[$1$2]+=$4 #field 1 and 2 are added and assigned to field 4.
POSSIBLE[$1$2]+=$5 #field 1 and 2 are added and assigned to field 5.
STUDENTS[$1]++ #adds one to itself to find the amount of students.
CATEGORIES[$2]++ #field 2 adds one to itself to find the amount of categories.
}
END{ #End block statement

# for(ASSIGNMENT in EARN) { #If statement that takes assignment field 4 value / field 5
# print ASSIGNMENT,EARN[ASSIGNMENT]/POSSIBLE[ASSIGNMENT]
# }


for (STUDENT in STUDENTS) {
SURVEY=(EARN[$1"Survey"]/POSSIBLE[$1"Survey"])*0.05 #breaking down each category for each student and finding
HOMEWORK=(EARN[$1"Homework"]/POSSIBLE[$1"Homework"])*0.10 #out their percentage for each category.Then I have to find the weighted
LAB=(EARN[$1"Lab"]/POSSIBLE[$1"Lab"])*0.30 #grade and print out the students percentage and letter grade.
QUIZ=(EARN[$1"Quiz"]/POSSIBLE[$1"Quiz"])*0.40
FINAL=(EARN[$1"Final"]/POSSIBLE[$1"Final"])*0.15
TOTAL=(SURVEY+HOMEWORK+LAB+QUIZ+FINAL)
LETTER="A"
if (TOTAL < 90) { #Simple if statement that starts LETTER of grade = A for 90#
LETTER="B" #and anything under 60 is an F.
}
if (LETTER < 80) {
LETTER="C"
}
if (LETTER < 70) {
LETTER="D"
}
if (LETTER < 60) {
LETTER="F"
}

for(ASSIGNMENT in EARN) {
print ASSIGNMENT,EARN[ASSIGNMENT]/POSSIBLE[ASSIGNMENT],TOTAL[ASSIGNMENT],LETTER[ASSIGNMENT] #if statement trying to print but
#syntax is probably off.
}
}
}

MadeInGermany 04-07-2020 12:40 AM

Here is a mistake:
Code:

  SURVEY=(EARN[$1"Survey"]/POSSIBLE[$1"Survey"])*0.05
In the END section there is no field $1 because it is after the input loop.
And what do you do with the STUDENT variable?

shruggy 04-07-2020 03:04 AM

Just nitpicking, but comments like
Quote:

Originally Posted by jessmister04 (Post 6108548)
EARN[$1$2]+=$4 #field 1 and 2 are added and assigned to field 4.

are not very helpful. Better would be
Quote:

# Scores($4) for each combination of Student($1) and Category($2) are added up.

grail 04-07-2020 11:49 AM

So this is far from definitive and I would have questions about what if the student does not have a mark for say Homework H07, this solution will only mark them out of 6 when it should be out of 7.

However, it gives you a broad idea on what you might do :)

Code:

#!/usr/bin/awk -f

BEGIN{
        FS = ","
        percentage["Homework"] = 10
        percentage["Lab"]      = 30
        percentage["Quiz"]    = 40
        percentage["Final"]    = 15
        percentage["Survey"]  = 5
}

NR > 1{
        category[$1][$2]++
        total[$1][$2] += $4 / $5
}

END{
        for(student in total){
                for( cat in total[student])
                        final_total[student] += (total[student][cat] / category[student][cat]) * percentage[cat]

                if (final_total[student] < 60)
                        grade = "F"
                else if (final_total[student] < 70)
                        grade = "D"
                else if (final_total[student] < 80)
                        grade = "C"
                else if (final_total[student] < 90)
                        grade = "B"
                else
                        grade = "A"

                print student, final_total[student], grade
        }
}

Have fun :)

jessmister04 04-07-2020 12:23 PM

Very nice, easy to read code, Grail. Well, the csv I was given has a mark for every assignment, quiz, lab, final, survey, etc. You know better than me but probably an if statement can fix that issue if there was no mark, but that is just my beginner thought process inside me guessing and talking. Anyway, Thank you so much for your response and help. I am going to continue and play with all of this code. I have a zoom meeting tomorrow and will be getting trained for about 5 hours straight. I am hoping to finish early so i can turn this project in tomorrow. That would give me the opportunity to go back and refresh up on bash, grep and sed before jumping into Perl. I will keep working on this and post my final work in the next few days. Again, thank you for your time grail, shruggy, MadeInGermany, and syg00.

jessmister04 04-07-2020 01:45 PM

Grail, so I am wondering if I may pick your brain a little to understand the script. I am pretty sure I got the first 3 questions right but would like to confirm. Also, I am not too experienced with for loops and was wondering if you can enlighten me of what is exactly going on inside the loop? I ran the script with my csv but received an error code, illegal statement at source line 18.

1.) Just want to confirm that percentage is an array that contains all the students categories and each category is set to the weighted total?
2.) Then you create a variable for category and set category so that it adds up all the category fields for each student?
3.) Then create a variable to calculate the total points each student received for the entire semester and divide it by field 5, the total points possible?
4.) The for loop I do not entirely understand. Sorry but in the if statements are you telling cat to output or is cat short for category? Sorry for my ignorance

BW-userx 04-07-2020 02:46 PM

what part of CODE tags didn't you understand? No wonder you're having problems in class, pay attention to instructions.

jessmister04 04-07-2020 03:09 PM

BW, do you judge every book by its cover and always sound this ignorant? If so, I feel sorry for you and your circle of people. I am trying my best and did not even know what UNIX was 8 weeks ago. Then you come out of the gate trashing me for not paying attention in class? For your information, this is for class, and my instructor will explain a lot of information to me tomorrow via zoom. AND, WE have no classroom, and we have no class. We simply are given Labs and Homework like this to complete and when we need help we are suppose to use Zoom with the instructor or reach out for help online. Sorry, for reaching out to a professional forum to try and build a foundation around a new passion. I don't mean to sound like a jerk but please be considerate of my experience. I have code I wrote this week that satisfies 85% of my homework and labs. This was the one part I was having trouble with.

jessmister04 04-07-2020 03:27 PM

I will learn code tags before posting anymore. Going to check out a 25 min youtube video and may be back

jessmister04 04-07-2020 03:35 PM

Also, Sorry BW, you're right. I need to slow down and follow instructions. I know that is a valuable lesson in programming and I appreciate your help even though I am struggling and got frustrated. I will learn code tags and maybe do another zoom session and then try and report back correctly.

shruggy 04-07-2020 03:57 PM

Quote:

Originally Posted by jessmister04 (Post 6108834)
I ran the script with my csv but received an error code, illegal statement at source line 18.

You probably copied it wrong. I ran grail's code with the csv you provided above and got
Code:

Chelsey 92.2143 A
Sam 19.0571 F


jessmister04 04-07-2020 04:03 PM

Okay, I rewrote it all on a mac terminal and might have a typo. Thank you for that, and I will get code tags down today. Never even heard of them until this week but I want to learn, understand, and code tags seem to help the educator.


All times are GMT -5. The time now is 12:33 PM.