Sponsored Content
Full Discussion: Loop doing calculations
Top Forums UNIX for Beginners Questions & Answers Loop doing calculations Post 303040607 by Eric7giants on Friday 1st of November 2019 04:09:20 PM
Old 11-01-2019
Loop doing calculations

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade for each student based on the scores and categories in the file. I am able to get the script to run the only issue however is that the same score for each student is the same. I'm self-teaching myself the command-line and am horrible at loops. What am I missing here.


Here is some sample data:
Code:
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


Here is my code:
Code:
BEGIN{
FS = ","
print "Name\tPercent\tLetter"
a=0
}

{

if(a==0){
        a+=1
}

else{
        sum[$1$2] += $4
        total[$1$2] += $5
        students[$1]++
        categories[$2]++

}
}

END{

for (b in students){
        Homework=(sum[$1"Homework"]/total[$1"Homework"])*0.10
        Lab=(sum[$1"Lab"]/total[$1"Lab"])*.30
        Final=(sum[$1"Final"]/total[$1"Final"])*.15
        Quiz=(sum[$1"Quiz"]/total[$1"Quiz"])*.40
        Survery=(sum[$1"Survey"]/total[$1"Survey"])*.05
        percent=(Homework+Lab+Final+Quiz+Survey)*100
        printf "%s\t%.2f\t",b,percent

        if(percent>=90 && percent<=100)
                print "A";

        else if(percent>=80 && percent<90)
                print "B";

        else if(percent>=70 && percent<80)
                print "C";

        else if(percent>=60 && percent<70)
                print "D";

        else
                print "E"



}

}

And the output:
Code:
Name    Percent    Letter
Andrew    72.64    C
Chelsey    72.64    C
Shane      72.64    C
Ava         72.64     C
Sam        72.64     C






table { }tr { }col { }br { }td { padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 12pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; }

Last edited by Eric7giants; 11-01-2019 at 05:18 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Float calculations

As expr is used for integer calculations, which command is used for float calculations. (1 Reply)
Discussion started by: sharmavr
1 Replies

2. UNIX for Dummies Questions & Answers

Time Calculations

I'm trying to have a loop print out statistics every X number of seconds. How can I add a specific number of seconds to a time variable and make a comparison? Thanks ahead of time. For example: startTime = `date +%H%M%S` currentTime = $startTime executeTime = startTime + X # X is equal... (5 Replies)
Discussion started by: Nysif Steve
5 Replies

3. Shell Programming and Scripting

ksh, calculations using bc

hi all, was wondering if there is another way to do calculations in ksh scripts other than using bc ?? i am using a script to calculate average response time and my script errors out after running for a bit. e.g code i am using : averageTime=$(print "$totalTime / $numberOfEntries" |... (2 Replies)
Discussion started by: cesarNZ
2 Replies

4. UNIX for Dummies Questions & Answers

Date Calculations

I need to be able to use the current date and calculate 7 days ago to be stored in another variable to be passed to a file in my Unix shell script. I need the date in the following format: date '+%m/%d/%Y' or 05/16/2006 How do I calculate date minus 7 days or 1 week ago? (8 Replies)
Discussion started by: mitschcg
8 Replies

5. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

6. UNIX for Dummies Questions & Answers

help with doing calculations on data

Dear All, I have a long list like this: 337 375 364 389 443 578 1001 20100 . . . . etc I would like to substract each value from the first entry which in this case is 337 and report it in a separate column. So the expected output looks like 337 0 (10 Replies)
Discussion started by: pawannoel
10 Replies

7. UNIX for Dummies Questions & Answers

Doing calculations with bc on one field

Hello, I have to turn: Apple Inc.:325,64:329,57 into Apple Inc.:325,64:329,57:3,93 3,93=329,57-325,64. My code: cat beurs.txt | sed 's/\(*\):\(*\),*\(*\):\(*\),\(*\)/\4\.\5-\2\.\3/' beurs.txt | bc| tr '.' ',' | sed 's/^-*,/0,/' > winstmarges.txt; paste -d: beurs.txt winstmarges.txt; rm... (5 Replies)
Discussion started by: ikke008
5 Replies

8. Shell Programming and Scripting

Problem with calculations

grep Quality abc.txt | awk -F"=" '{print $2}' o/p is given as 70/70 49/70 I want in the below format (percentage format) 100% 70% help me!!!!:confused::confused::confused: ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Cell 01 -... (3 Replies)
Discussion started by: nikhil jain
3 Replies

9. Shell Programming and Scripting

Number calculations

I'm writing a script that will read all the fields of a text file into an array(if they are numeric), while at the same time computing the minimum and maximum values from the file. After that I want to output the average of all the numbers in the array. The first problem I'm having is that many... (10 Replies)
Discussion started by: ksmarine1980
10 Replies

10. Shell Programming and Scripting

Output calculations

Attached are the is original output (zipped file) and a custom file using the awk code below in which the average reads per bait are calculated (average.txt) awk '{if(len==0){last=$4;total=$6;len=1;getline}if($4!=last){printf("%s\t%f\n", last,... (7 Replies)
Discussion started by: cmccabe
7 Replies
IO::Digest(3)						User Contributed Perl Documentation					     IO::Digest(3)

NAME
IO::Digest - Calculate digests while reading or writing SYNOPSIS
use IO::Digest; # Get a Digest::MD5 object that takes input while $fh being written or read my $fh; my $iod = IO::Digest->new ($fh, 'MD5'); print $fh "fooo"; print $iod->hexdigest DESCRIPTION
This module allows you to calculate digests while reading or writing file handles. This avoids the case you need to reread the same content to compute the digests after written a file. TEST COVERAGE
----------------------------------- ------ ------ ------ ------ ------ ------ File stmt branch cond sub time total ----------------------------------- ------ ------ ------ ------ ------ ------ blib/lib/IO/Digest.pm 100.0 n/a n/a 100.0 100.0 100.0 Total 100.0 n/a n/a 100.0 100.0 100.0 ----------------------------------- ------ ------ ------ ------ ------ ------ AUTHORS
Chia-liang Kao <clkao@clkao.org> COPYRIGHT
Copyright 2004 by Chia-liang Kao <clkao@clkao.org>. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <http://www.perl.com/perl/misc/Artistic.html> perl v5.18.2 2013-02-16 IO::Digest(3)
All times are GMT -4. The time now is 10:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy