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