problem invoking Awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem invoking Awk script
# 1  
Old 04-26-2009
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
Code:
awk -f report products associates sales

But so far when ever I do that errors are produced such as:
Code:
awk: report:2: awk '/2008/{print $0}' sales > /tmp/newSales
awk: report:2:       ^ invalid char ' ' ' in expression



When I invoke the script with
Code:
./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
Code:
#! /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
# 2  
Old 04-26-2009
messing around

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.
Code:
#! /bin/bash
awk "/2008/ {print $0}" sales > /tmp/newSales

If i change it to
Code:
#! /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
# 3  
Old 04-26-2009
Quote:
Originally Posted by angermanaged
...
Can someone explain to me why this happens ...
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:

Code:
$
$ # 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:

Code:
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.

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


Quote:
... and how to go upon converting the file so it works.
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.

Code:
$
$ # 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."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Invoking system(cmd) inside awk command

Hi, I was searching for a way to grep 2 lines before and after a certain keyword, and I came across the following code.. awk "\$0 ~ /ORA-/ { cmd=\"awk 'NR>=\" NR-2 \" && NR<=\" NR+2 \"' init.ora\" system(cmd) }" input_file I could not understand how this works. What is system() ? what... (2 Replies)
Discussion started by: Kulasekar
2 Replies

2. Shell Programming and Scripting

Invoking Application in Shell Script

Hi All, I have a scenario : 1. A list of servers naming server21, server 22, server 23 etc. This list of servers is separate for my environments. Env1 has 3 server Env2 has 5 serves Env3 has 10 servers 2. Each server accesses application through which I want to invoke some method. So... (7 Replies)
Discussion started by: ankur328
7 Replies

3. Shell Programming and Scripting

Problem invoking shell script

whats wrong here?! ________________ #!/bin/sh # FILE: clean_dirs # GOAL : To clean up directories used in Oracle processing # LANGUAGE : shell script (sh) # PARAMETERS : Input_File # 11/07/02 acri - modified to cd into the directory so it will... (7 Replies)
Discussion started by: gillraj
7 Replies

4. UNIX for Dummies Questions & Answers

invoking script

hi all, is there a way to run a script upon invoking an application . for eg if i click on mozilla i want a script to run , before runniing mozilla , maybe ask a password or something only then open mozilla (2 Replies)
Discussion started by: mithun1!
2 Replies

5. Shell Programming and Scripting

Invoking a script

I am new to Unix. Could some tell me what are all the possible ways of invoking/executing a script, doesnt matter which shell you are in. Thanks (4 Replies)
Discussion started by: bobby1015
4 Replies

6. Shell Programming and Scripting

Help with invoking sed from a script

Hello I have a .sh script that needs to take a template and replace zzz in the template file with the user's name. I cannot figure out how to make the following work from my script (works from the command prompt) but not when I put it inside a *.sh script sed 's/xxx/$USER/g' template.conf >... (2 Replies)
Discussion started by: kasiolas
2 Replies

7. Shell Programming and Scripting

pass the input to invoking script

How to pass the input to the execution script ex: test1.sh contains #! usr/bin/sh read val echo $val test2.sh contains #! /usr/bin/sh ./test1.sh now I am calling test2.sh thro command line and I want to pass the input to the script test1.sh from test2.sh ..I mean not... (6 Replies)
Discussion started by: vastare
6 Replies

8. Shell Programming and Scripting

returning to the parent shell after invoking a script within a script

Hi everybody, I have a script in which I'm invoking another script which runs in a subshell. after the script is executed I want to return to the parent shell as some variables are set. However i'm unable to return to my original shell as the script which i'm calling inside is invoked in... (5 Replies)
Discussion started by: gurukottur
5 Replies

9. Shell Programming and Scripting

Invoking Shell Script via php

list me commands to invoke a shell script from php once the submit button is clicked in the php page. Requirement is Once a submit button is clicked it should run a script that displays the outcome of the script in a html/php. Please help. Thanks in Advance, BubeshJ (2 Replies)
Discussion started by: bubeshj
2 Replies

10. Shell Programming and Scripting

invoking one shell script from other

hi, i am one day old in shell scritpting. how to invoke one shell script from the other? For eg.i have two shell scripts A.sh and B.sh. Inside A.sh i need to invoke B.sh and the return code of A.sh should be the value returned by B.sh. it would be better if you provide any sample shell... (3 Replies)
Discussion started by: ajay xavier
3 Replies
Login or Register to Ask a Question