Bourne Script, sorting and getting average of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bourne Script, sorting and getting average of file
# 1  
Old 03-17-2011
Bourne Script, sorting and getting average of file

Hi I am trying to get a bourne shell script to sort a file for me. Here is what i currently have:
Code:
#/bin/sh
echo "Name   Exam1   Exam2   Exam3  Total  Grade" > final.txt
awk -f 9.awk grades.txt | sort +4 -5 >> final.txt

Code:
#BEGIN {printf "Name\tExam1\tExam2\tExam3\tTotal\tGrade";}
{
        if($2+$3+$4>90) {printf "%s\t%d\tA\n",$0,$4+$2+$3;}
        else
                if(($2+$3+$4>=80)&&($2+$3+$4<90)) {printf "%s\t%d\tB\n",$0,$2+$3+$4;}
                else
                if(($2+$3+$4>=70)&&($2+$3+$4<80)) {printf "%s\t%d\tC\n",$0,$2+$3+$4;}
                        else
                if(($2+$3+$4>=50)&&($2+$3+$4<70)) {printf "%s\t%d\tD\n",$0,$2+$3+$4;}
                  else {printf "%s\t%d\tF\n",$0,$2+$3+$4;}
}


This is what I want it to look like when done:
Code:
Name Exam1 Exam2 Exam3 Total Average Grade
Brian   38   38   40   116   96   A
Jack    30   35   38   103   85   B
Lisa    30   28   32   90    75   C
Nick    33   30   15   78    65   C
Jane    31   25   19   75    62   D
Tom     23   12   30   65    54   D
Karen   25   28   12   65    54   D

and this is what it looks like right now:
Code:
Name   Exam1   Exam2   Exam3  Total  Average  Grade
Name	Exam1	Exam2	Exam3	0	F
Jack	30	35	38	103	A
Brian	38	38	40	116	A
Karen	25	28	12	65	D
Tom	23	12	30	65	D
Jane	31	25	19	75	C
Nick	33	30	15	78	C
Lisa	30	28	32	90	F

right now it is sorting on the total column. which is wrong,I know I need to have it sort on the average column, however I cant get it to display an average. How do i get it to calculate the average? Also I have the extra line of Name Exam1.. etc that I cant get rid of.

Any help would be much appreciated.
# 2  
Old 03-17-2011
to sort of 'average' (the 6-th field):
Code:
sort -k6,6

# 3  
Old 03-17-2011
Thanks Smilie First I need to know how to get the script to calculate the average.
# 4  
Old 03-17-2011
awk -f 9.awk OFS='\t' grades.txt | sort -k6,6
9.awk:
Code:
#BEGIN {printf "Name\tExam1\tExam2\tExam3\tTotal\tGrade";}
{
        sum=$2+$3+$4
        printf("%s%c%d%c%d%c", $0,OFS,sum,OFS,sum/3,OFS)
        if(sum>90) {print "A"}
        else
                if((sum>=80)&&(sum<90)) {print "B"}
                else
                if((sum>=70)&&(sum<80)) {print "C"}
                        else
                if((sum>=50)&&(sum<70)) {print "D"}
                  else {print "F"}
}

# 5  
Old 03-17-2011
cool thanks Smilie
so now it displays
Code:
Name    Exam1   Exam2   Exam3   0       0       F
Karen   25      28      12      65      21      D
Tom     23      12      30      65      21      D
Jane    31      25      19      75      25      C
Nick    33      30      15      78      26      C
Lisa    30      28      32      90      30      F
Jack    30      35      38      103     34      A
Brian   38      38      40      116     38      A

Any suggestions on how to remove the 0's up top and replace them with text saying Total, average, and grade?
# 6  
Old 03-17-2011
Code:
echo "Name   Exam1   Exam2   Exam3  Total  Average Grade" > final.txt

# 7  
Old 03-17-2011
I wish that worked Smilie everything else works except for the top text and the remaining text on the bottom. is there a way to clear a line if the value is 0?

Code:
Brian	38	38	40	116	96	A
Jack	30	35	38	103	85	B
Lisa	30	28	32	90	75	C
Nick	33	30	15	78	65	C
Jane	31	25	19	75	62	D
Tom	23	12	30	65	54	D
Karen	25	28	12	65	54	D
Name	Exam1	Exam2	Exam3	0	0	F

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed: script for timely average from log file

Please repost your query: Help needed: script for timely average from log file - Thank you. (0 Replies)
Discussion started by: mkfs
0 Replies

2. Shell Programming and Scripting

Script to load daily average I/O stats from a .ksh file into Oracle db

Hi can anyone help me with a script to load output of the .ksh file into an Oracle database. I have attached sample output of the information that i need to load to the database (2 Replies)
Discussion started by: LucyYani
2 Replies

3. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. Shell Programming and Scripting

Open a file from within a Bourne shell Script

I am suppose to make a program that cuts the extension off of a file. It uses that .ext to decide which program needs to be used to open the file. This only finds the name of the program from a list of programs I made. My problem is when it has the name of the program it needs to use, how can I... (3 Replies)
Discussion started by: dordrix
3 Replies

5. Shell Programming and Scripting

Calculate average from CSV file using PERL script

Hi All I have this csv file and I need to calculate the average of FPS. FPS:27.7420, Interval:1314184238772 FPS:25.9798, Interval:1314184242646 FPS:27.4772, Interval:1314184246311 FPS:26.1623, Interval:1314184250159 FPS:26.4515, Interval:1314184253972 FPS:31.5896, Interval:1314184257163... (24 Replies)
Discussion started by: sayachop
24 Replies

6. Shell Programming and Scripting

help with bourne script

Hey guys not sure why but when i execute the script i get the correct result but then it says command not found not sure why can anyone see anything wrong with my code below? I just want to print how much quota i have used in my home directory #!bin/sh `quota -v | grep ^/home | awk... (2 Replies)
Discussion started by: musicmancanora4
2 Replies

7. Shell Programming and Scripting

Bourne Script help

Hey guys, I am trying to do a bourne script to look for c files in the current directory. I had it working where it finds the files and asks you to delete them or not, which works, but if there i no files, then it comes up with errors, which iam trying to get rid of. So I thought I would do a if... (2 Replies)
Discussion started by: Pits
2 Replies

8. Shell Programming and Scripting

bourne script help

I need to make a small script that figures out if a filename that the user enters is a file or a directory. and if it is a directory, how many files are in it. please point me to the right direction, I am a newbie at this. (1 Reply)
Discussion started by: Heedunk
1 Replies

9. Shell Programming and Scripting

simple bourne script

Hello There, I am trying to write this SIMPLE script in Bourne Shell but I keep on getting a blank response. Can you see what I am doing wrong? I am simply trying to take the day of the week from our system and when the teachers sign on I want them to see the message of the day, when they exe... (2 Replies)
Discussion started by: catbad
2 Replies

10. UNIX for Advanced & Expert Users

Bourne shell script need help please ?

i have this assignment.. and i mad this script but there is something wrong with it.. if anyone can tell me.. watz going on... i would appreciate it.. tHnX in advance.. count=1 val=$2 op=$1 ans=0 if then if then while do ... (7 Replies)
Discussion started by: dezithug
7 Replies
Login or Register to Ask a Question