Please help me with awk to output list of students, their grade as % and letter grade
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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]
}
}
}
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-MacBookesktop 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.
}
}
}
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
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.
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, 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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.