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
# 8  
Old 03-17-2011
show exactly what the script looks like and your sample input file.
# 9  
Old 03-17-2011
here is the input file :
Code:
Name	Exam1	Exam2	Exam3
Tom	23	12	30
Jack	30	35	38
Jane	31	25	19
Brian	38	38	40
Lisa	30	28	32
Nick	33	30	15
Karen	25	28	12

Code:
#BEGIN {printf "Name\tExam1\tExam2\tExam3\tTotal\tAverage\tGrade";}
{
sum=$2+$3+$4
grade = sum * 2.5
avg = grade / 3
		
        printf("%s%c%d%c%d%c", $0,OFS,sum,OFS,avg,OFS)
        if(avg>=90) {print "A"}
        else
                if((avg>=80)&&(avg<90)) {print "B"}
                else
                if((avg>=65)&&(avg<80)) {print "C"}
                        else
                if((avg>=50)&&(avg<65)) {print "D"}
                  else {print "F"}
}

Code:
#/bin/sh
echo "Name   Exam1   Exam2   Exam3  Total Average  Grade" > finalgrades.txt
awk -f 9.awk OFS='\t' grades.txt | sort -k6,6 -r > finalgrades.txt

# 10  
Old 03-17-2011
9.awk:
Code:
#BEGIN {printf "Name\tExam1\tExam2\tExam3\tTotal\tAverage\tGrade";}
# skip the header line from the .txt file
FNR>1{
sum=$2+$3+$4
grade = sum * 2.5
avg = grade / 3

        printf("%s%c%d%c%d%c", $0,OFS,sum,OFS,avg,OFS)
        if(avg>=90) {print "A"}
        else
                if((avg>=80)&&(avg<90)) {print "B"}
                else
                if((avg>=65)&&(avg<80)) {print "C"}
                        else
                if((avg>=50)&&(avg<65)) {print "D"}
                  else {print "F"}
}

# 11  
Old 03-17-2011
Cool now it doesnt display the crappy header Smilie Thanks so much. Now I am going to work on getting the real header to work.
# 12  
Old 03-17-2011
Quote:
Originally Posted by DualPandas
Cool now it doesnt display the crappy header Smilie Thanks so much. Now I am going to work on getting the real header to work.
you don't want to have the 'crappy' header from the original file - it will screw up your sorting AND it will be invalid as you're adding more columns.
that's why you create a new/non-crappy header in the shell wrapper - not in the 'awk' script itself.
Unless I'm missin' some'....
# 13  
Old 03-17-2011
Thanks for all your help Smilie
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