awk command in script gives error while same awk command at prompt runs fine: Why?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command in script gives error while same awk command at prompt runs fine: Why?
# 1  
Old 09-20-2010
awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all,

Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val:

Code:
> cat getnon0file.sh
#!/bin/bash
this="getnon0file.sh"
USAGE=$this" [filename threshold_value]
InFile="xyz.38"
Min="0.05"
#[ I removed the input check to shorten the code]

awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n}; tot=(NF+1);$tot=sum; if(sum>=0.05); {print}}' $InFile > $InFile.non0

echo ----  $this over ---  Result saved into $InFile.non0

When I call the script at the prompt, I get an error:
Code:
> getnon0file.sh xyz.38 0.05
/path/getnon0file.sh: line 36: syntax error near unexpected token `('
/path/getnon0file.sh: line 36: `awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n}; tot=(NF+1);$tot=sum; if(sum>=0.05); {print}}' $InFile > 
$InFile.non0'

When I run the awk command alone at the prompt, I obtain the results I want
Code:
> awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n}; tot=(NF+1);$tot=sum; if(sum>=0.05); {print}}' xyz.38 > xyz.38.non0

Is there any way around what seems to me an attractable error?

Thanks.

Just in case you need it, the datafile (xyz.38) look like this:

Code:
code1_001 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.008 0.028 0.198 0.502 0.625
code1_002 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.009 0.066 0.158 0.190
code1_003 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.009 0.060 0.148 0.185
code1_004 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.992 0.954 0.676 0.192 0.000
code2_001 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.008 0.055 0.229 0.390
code2_002 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.004 0.024 0.108 0.193
code2_003 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.029 0.118 0.209


Last edited by radoulov; 09-20-2010 at 06:14 PM.. Reason: Code tags, please!
# 2  
Old 09-20-2010
The error could be anywhere. If it's not the missing quote here:

Code:
 
USAGE=$this"

... post/attach the entire script.
This User Gave Thanks to radoulov For This Post:
# 3  
Old 09-20-2010
Thanks for your good eyes, radoulov;
Bad quoting was the problem.
# 4  
Old 09-20-2010
The awk command can be shorter.

Code:
awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};  if(sum>=0.05) print $0,sum}' $InFile

# 5  
Old 09-20-2010
That's right!
Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script with awk command ERROR

Hello im new here... Im trying to read file and create folders from words in it but i get this for loop error awk : line 3 : syntax error at or near for my code is.. #!/bin/bash begin for (( i=1;i<=5;i++)); do awk -v i=$i $0 { print $i } mkdir $i done {print $i} end {} i have... (7 Replies)
Discussion started by: boxstep
7 Replies

2. Shell Programming and Scripting

Script runs in command-line fine but times out in CRON?

Hi, I have a script that seems to run to completion when in the command-line, but when it is run using the cron, it seems to time out. They both start and run fine, but on the CRON it stops prematurely. The script hits an API every few seconds and grabs data. Does anyone have any idea on... (4 Replies)
Discussion started by: phpchick
4 Replies

3. Shell Programming and Scripting

Getting Command not found error Even though Script is working fine

Hi friends, I am using below script to do some work. But even though script is working fine but while executing it i am getting command not found error. :( Here is the script :- #!/bin/sh Names="name.txt" ###main##### for LINE in `cat ${Names}` do ... (3 Replies)
Discussion started by: harpal singh
3 Replies

4. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

5. Shell Programming and Scripting

Script runs fine manually but not in crontab

Hello Guys, I have scratched my head alot on this but couldn't find clue what's wrong. Can you please help me with this? My problem is as following. 1) When I manually execute following script it runs successfully with below output. bash-3.00# more smssend #!/bin/bash echo -e "<Request... (16 Replies)
Discussion started by: umarsatti
16 Replies

6. Shell Programming and Scripting

Shell script runs fine in Solaris, in Linux hangs at wait command

HI, I have a strange problem. A shell script that runs fine on solaris. when i ported to linux, it started hanging. here is the core of the script CFG_FILE=tab25.cfg sort -t "!" -k 2 ${CFG_FILE} | egrep -v "^#|^$" | while IFS="!" read a b c do #echo "jobs output" #jobs #echo "jobs... (13 Replies)
Discussion started by: aksaravanan
13 Replies

7. Programming

getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program int main() { char str; FILE * fp; int i=0; ... (4 Replies)
Discussion started by: bhavesh.sapra
4 Replies

8. Shell Programming and Scripting

Script Runs fine but not giving any output

Hi, My script is running with no erros but not giving any output can anyonehelp. #!/bin/ksh . /home/application/bin/application.env OUTFILE=Result.txt PROD_PASSWORD=`${GET_PWD} -f ${PWD_FILE_PATH} -s ${PROD_SERVER} -u ${PROD_USER}` echo "1)To get the book last loaded details " read... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

9. Shell Programming and Scripting

Script runs fine, but not in a cron

Okay, I have the following script that runs fine from a command line as well as an executable .sh file. It just moves any file/folder with movie* in the name to a folder called _Movies. The issue I'm running into is when it's call from a cron. find /mnt/HD_a2/BT/complete -iname "movie.*" -exec... (4 Replies)
Discussion started by: sammyk
4 Replies
Login or Register to Ask a Question