954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bash/awk script

This is homework. I have to write a bash shell script using awk to process three delimited text files and produce a formatted report. The three files are as follows:

GRADES file
John Bunyan:90:100:75:60
Maria Montessori:80:90:60:82
Alice Cooper:75:80:54:47
Harvey Mudd:89:45:89:95

PROJECTS file
test1:30
test2:30
paper2:20
exam:50

LEVELS file
A:89.5
B:79.5
C:69.5
D:59.5
F:0


I hope this is fairly easy to see how these relate. The object is to go through each student, weight their grades, and output the corresponding letter grade. The awk script I have so far looks like it should work, but I am getting numerous errors, mostly of a few types. Here is the script:

#!/bin/bash

FILEGRADES=grades
FILEPROJ=projects
FILELEVEL=levels


awk '
	titleline[0]="Name"
	z=1
	while ( (getline < "FILEPROJ") > 0) {
		titleline[z]=$1
		weights[m]=$2
		z++
		m++
	}
	for (n = 0, n < m, n++) {
		ttlwght=$ttlwght+$weights[n]
	}
	titleline[z]="average"
	for (y = 0, y < z, y++){
		print $titleline[y]
		print "\t"
	}

	while ( (getline < "FILELEVEL") > 0) {
		gradelvl[a]=$1
		gradereq[a]=$2
		a++
	}

	while ( (getline < "FILEGRADES") > 0) {
		for(i = 2, i <= NF, i++){
			gradesum=$i*$weights[i-1]
		}
		fingrade=$gradesum/$ttlwght
		print $fingrade
		for(c = 0,c < a,c++) {
			if (fingrade>gradereq[c]) {
				then
					print gradelvl[c]
			else
			fi
			}
		}
' FILEGRADES FILEPROJ FILELEVEL


The errors I am getting are of only a few types:
(1) Syntax error (beginning and end of each loop declaration (at the "for" or "while" and at the "{" bracket))
(2) 'unexpected newline or end of string' (end of every assignment option like "gradelvl[a]=$1", and also end of the statement "print gradelvl[c]")

Not sure what I'm doing wrong, but if anyone can help, that would be great.
Thanks.

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

There are so many problems with this script I don't know where to start. I tried to correct it and came to the conclusion that it would be easier to rewrite it from scratch.

Have a look at a AWK tutorial - http://www.grymoire.com/Unix/Awk.html seems to cover the basics pretty well.

shibblez
Junior Poster in Training
72 posts since Oct 2010
Reputation Points: 15
Solved Threads: 6
 

Review the tutorial shibbelz posted. I *still* use a 1988 edition of "The AWK Programming Language" as my primary (only) AWK reference manual.

Without finishing the project for you (meaning the following will be somewhat cryptic):The whole program must be in {} braces.
Compound statements are enclosed in {}; single statements are terminated with ';'
The elements of for() are statements; terminate as such.
If/then/else/fi is shell syntax, not awk.
You haven't specified a field separator to auto-split the data. The split() function is an explicit alternative.
Write your own HOWTO: write a simple awk program to just read and split the fields of each file, and print them.
You use uninitialized values; while this may be OK in awk, it's bad programming form.
Re-examine your data structures. You aren't storing the 'projects' data in a useful manner.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: