problem invoking Awk script

I have been writing an awk script to calculate and report on sales numbers for a couple different files. The only problem i am having is when i try to call the script.
I want to invoke the script by way of

awk -f report products associates sales

But so far when ever I do that errors are produced such as:

awk: report:2: awk '/2008/{print $0}' sales > /tmp/newSales
awk: report:2:       ^ invalid char ' ' ' in expression

When I invoke the script with

./report associates products sales

It works perfectly

Can someone explain to me why this happens and how to go upon converting the file so it works.

Here is the script file

#! /bin/bash
awk '/2008/ {print $0}' sales > /tmp/newSales

declare -a names
declare -a totalsales
declare -a position

echo -e "Name:\tPosition:\tTotal Sales:"
ID=1
while [ $ID -le 6 ]
do
  awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
  let ID=$ID+1
done

GRANDTOTAL=0
ID=1
while [ $ID -le 6 ]
do
  while read line
  do
    PRODID=`echo $line|cut -d"," -f1`
    QUANT=`echo $line|cut -d"," -f2`
    PRICE=`awk /^$PRODID/ products|awk -F":" '{print $3}'`
    TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
    GRANDTOTAL=$(echo "scale=2; $GRANDTOTAL+$TOTAL" | bc)
done < "/tmp/sales.2$ID"

totalsales[$ID]=$GRANDTOTAL
NAMES[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $2}'`
position[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $4}'`

echo ${NAMES[$ID]}":"${position[$ID]}":"${totalsales[$ID]} >> /tmp/output.$$

GRANDTOTAL=0
let ID=$ID+1
done

sort -n /tmp/output.$$ | awk -F":" '{print $1 $2 $3}' #/tmp/output.$$

# removes all created files
rm /tmp/sales.2*
rm /tmp/newSales
rm /tmp/output.$$

Thank you all who try to help me

Ive been changing some things around and trying to get this to work. Is there something wrong with this first line cause the compiler seems to think there is one.

#! /bin/bash
awk "/2008/ {print $0}" sales > /tmp/newSales

If i change it to

#! /bin/bash
awk "/2008/ {print $0}" sales > /tmp/newSales

i get an error further down the program complaining about the syntax of the while loop which I know there isn't a problem with.

anyone have any ideas

This happens because you are trying to invoke a shell script with the "-f" option of the awk command line.
The awk interpreter must be able to understand the contents of the script that follows "-f" option. But that script, in your case, contains an awk command itself. That is the reason awk balks. It is demonstrated below:

$
$ # display contents of file "sales"
$
$ cat sales
2007 - line no. 1
2007 - line no. 2
2008 - line no. 1
2008 - line no. 2
2008 - line no. 3
2009 - line no. 1
$
$ # display contents of shell script "report"
$
$ cat report
#!/bin/bash
#
# Note that this is a *shell* script, and not an awk script.
# The shebang at line 1 makes this script run in bash shell.
#
awk '/2008/ {print $0}' sales > newsales
$
$
$ # now execute the shell script; note that "sales" need not be passed as argument
$
$ ./report
$
$ # check if it worked
$
$ cat newsales
2008 - line no. 1
2008 - line no. 2
2008 - line no. 3
$
$

So far, so good. However, you cannot feed a line like the following:

awk '/2008/ {print $0}' sales > newsales

to the awk interpreter and expect it to understand it; which is why the script fails *within* awk interpreter.

$
$ awk -f report sales
awk: report:6: awk '/2008/ {print $0}' sales > newsales
awk: report:6:     ^ invalid char ''' in expression
$

I haven't checked your entire script; but it seems like you do need a *shell* script instead of an *awk* script. Many of the operations like sorting files and removing files are best performed in the shell rather than awk.

As far as just the first command is concerned (i.e. finding out all lines that contain "2008" in the file "sales" and redirecting them to "newsales"), you can accomplish the same using the following awk script.

$
$ # show the contents of awk script "report.awk"
$
$ cat report.awk
#
# This, on the other hand, is an awk script, and not a shell script.
# There is no shebang at line 1 and this script can be invoked with
# the "-f" option of the awk command line.
#
/2008/ { print $0 }

$
$ # invoke "report.awk" with the "-f" option of awk
$ # note that we must pass "sales" file as the input
$
$ awk -f report.awk sales
2008 - line no. 1
2008 - line no. 2
2008 - line no. 3
$
$
$ # and if redirection is to be done, do it in the shell
$
$ awk -f report.awk sales > newsales
$
$ cat newsales
2008 - line no. 1
2008 - line no. 2
2008 - line no. 3
$
$

Hope that helps,
tyler_durden

____________________________________________________
"Only after disaster can we be resurrected."