LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-06-2020, 06:09 PM   #1
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Rep: Reputation: Disabled
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]

}
}
}
 
Old 04-06-2020, 06:38 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,133

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
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]
             
        }
}
}
 
Old 04-06-2020, 06:56 PM   #3
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-06-2020, 07:17 PM   #4
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
}
}
}
 
Old 04-07-2020, 12:40 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
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?
 
Old 04-07-2020, 03:04 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Just nitpicking, but comments like
Quote:
Originally Posted by jessmister04 View Post
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.
 
Old 04-07-2020, 11:49 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
Old 04-07-2020, 12:23 PM   #8
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-07-2020, 01:45 PM   #9
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-07-2020, 02:46 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
what part of CODE tags didn't you understand? No wonder you're having problems in class, pay attention to instructions.
 
Old 04-07-2020, 03:09 PM   #11
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-07-2020, 03:27 PM   #12
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
I will learn code tags before posting anymore. Going to check out a 25 min youtube video and may be back
 
Old 04-07-2020, 03:35 PM   #13
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-07-2020, 03:57 PM   #14
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by jessmister04 View Post
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
 
Old 04-07-2020, 04:03 PM   #15
jessmister04
LQ Newbie
 
Registered: Apr 2020
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply

Tags
awk, code, csv



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Students compete for a chance to have their Raspberry Pi code run in space LXer Syndicated Linux News 0 04-06-2015 08:01 AM
LXer: How computer science teachers can better reach their students LXer Syndicated Linux News 0 01-16-2014 05:00 PM
LXer: Students map their university campus with MapKnitter LXer Syndicated Linux News 0 07-18-2013 04:51 PM
LXer: Junior High School students build their own Ubuntu computers LXer Syndicated Linux News 0 03-01-2010 09:10 AM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 07:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration