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.

Recommended Answers

All 2 Replies

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.

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):

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

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.