Sum working but getting syntax error - awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum working but getting syntax error - awk
# 1  
Old 07-18-2017
Sum working but getting syntax error - awk

Hi,

I am using the following command to get the sum and it is working correctly but I am getting syntax error as well.

Code:
# ------------------------------------------------------------------------
# Adding awk command support for SunOS -- use nawk for SunOS
# ------------------------------------------------------------------------
case `uname` in
  SunOS) awkcmd=nawk ;;
  *) awkcmd=awk ;;
esac

sum=`"$awkcmd" ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc`

(Tried with bc -l as well)

Syntax error received for every line numbers.
(standard_in) 3: syntax error

Thanks

Last edited by vgersh99; 07-18-2017 at 10:15 AM.. Reason: code tags, please!
# 2  
Old 07-18-2017
why do you need bc?
Code:
sum=$(${awkcmd} '{asum += $1} END {printf ("%.4f\n", asum)}' temp.hash.txt)

# 3  
Old 07-18-2017
Quote:
Originally Posted by uuuunnnn
...
...
I am using the following command to get the sum and it is working correctly but I am getting syntax error as well.

Code:
# ------------------------------------------------------------------------
# Adding awk command support for SunOS -- use nawk for SunOS
# ------------------------------------------------------------------------
case `uname` in
  SunOS) awkcmd=nawk ;;
  *) awkcmd=awk ;;
esac

sum=`"$awkcmd" ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc`

(Tried with bc -l as well)

Syntax error received for every line numbers.
(standard_in) 3: syntax error

...
While I agree with vgersh99 that you don't need bc when you are using awk anyway (since awk can perform the summation itself), I was curious to know what exactly is causing the error.

Looks like you are generating a bunch of "bc" commands that you pipe into bc.
With a sample file that has floating point numbers in it, I tried your code in my system that consists of Cygwin 1.6.10, GNU bash 3.2.39, GNU awk 3.1.6, GNU bc 1.06.

Code:
$
$ cat temp.hash.txt
1.1 the
2.2 quick
3.3 brown
4.4 fox
5.5 jumps
$
$ awk ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt
sum=0
sum += 1.1
sum += 2.2
sum += 3.3
sum += 4.4
sum += 5.5
sum
scale=4
sum=sum/1
$
$ awk ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc
16.5
$
$

And inside a shell script, the logic works well:

Code:
$
$ cat awksum.sh
# ------------------------------------------------------------------------
# Adding awk command support for SunOS -- use nawk for SunOS
# ------------------------------------------------------------------------
case `uname` in
  SunOS) awkcmd=nawk ;;
  *) awkcmd=awk ;;
esac
 sum=`"$awkcmd" ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc`
echo "sum = $sum"
$
$ . awksum.sh
sum = 16.5
$
$

So it looks like there is something wrong with the data in that "temp.hash.txt" file.
Apparently, "bc" balks at line number 3:

Code:
(standard_in) 3: parse error

which would be the line in red below:

Code:
sum=0
sum += 1.1
sum += 2.2
sum += 3.3
sum += 4.4
sum += 5.5
sum
scale=4
sum=sum/1

So if I change the 2nd line of "temp.hash.txt", I am able to reproduce your error:

Code:
$
$ cat temp.hash.txt
1.1 the
2.a quick
3.3 brown
4.4 fox
5.5 jumps
$
$ awk ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt
sum=0
sum += 1.1
sum += 2.a
sum += 3.3
sum += 4.4
sum += 5.5
sum
scale=4
sum=sum/1
$
$ awk ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc
(standard_in) 3: parse error
14.3
$
$

In the shell script as well:
Code:
$
$ cat awksum.sh
# ------------------------------------------------------------------------
# Adding awk command support for SunOS -- use nawk for SunOS
# ------------------------------------------------------------------------
case `uname` in
  SunOS) awkcmd=nawk ;;
  *) awkcmd=awk ;;
esac
 sum=`"$awkcmd" ' BEGIN {print "sum=0"} {print "sum += " $1; asum += $1} END {printf "sum\nscale=4\nsum=sum/1\n"}' temp.hash.txt | bc`
echo "sum = $sum"
$
$ . awksum.sh
(standard_in) 3: parse error
sum = 14.3
$
$

So I'd guess that maybe line # 2 of your temp.hash.txt does not have an integer or float(?)
# 4  
Old 07-18-2017
Thanks everyone for the reply. durden_tyler 's comments helped me. Thanks.

The issue was in the temp.hash.txt, we encountered null values and the sum was not getting computed and resulted in parsing error. The comment from "durden_tyler" was on the same note about the way he reproduced the error by placing a character instead of number and that resulted in syntax error.

Now to the point why I did not use the command
Code:
sum=$(${awkcmd} '{asum += $1} END {printf ("%.4f\n", asum)}' temp.hash.txt)

This is important for everyone to know why I had to go this route. Actually I was using the above command earlier.

The issue for the above command rises from the difference between float and decimal.
The problem with float number addition is once it goes beyond or less than a significant number, it refers arithmetic log values. You may have noticed this in excel as well when you get numbers in the format of “78768898E+11” or “676767E-4” (an example which you can find in excel).

I never got any number with E+xxxx or E-xxxx like in excel however I was getting random numbers in 3rd and 4th decimal places because I have a huge file with only 2 decimal point numbers.

You can understand with 2 decimal places numbers, it can never have 3rd and 4th decimal place numbers but with usage of
Code:
sum=$(${awkcmd} '{asum += $1} END {printf ("%.4f\n", asum)}' temp.hash.txt)

-- I was getting such result and there was a reconciliation issue with a database decimal value.

Last edited by Scrutinizer; 07-18-2017 at 11:31 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: syntax error

awk -F, ' NR>1 { BEGIN{ chr=$2 } END{ for (k in chr) {print k} } } ' $gene_file I've a really simple code. I want to store gene name and it's chromosome and in the end print them. I'm skipping line one as it contains headers. But I don't know why I get error as: (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

Syntax Error in AIX but working in UX

MENU_INTRO(){ date="`date`" HOSTNAME="`hostname`" if ; then cols=$2 else cols=2 fi clear now=`date +%A,%B-%d-%Y@%H:%M:%S` echo " -: INFORMIX DBA MENU :- " echo... (1 Reply)
Discussion started by: fedora132010
1 Replies

3. Shell Programming and Scripting

awk syntax error

Can anyone work out why this line has a syntax error? awk '{if ($1==1) print NR,$0 } '${PROJECT}/${data_dir}/${ofolder}/STDEV/otimes_${per}_secs.dat > tmp2.txt I've check that the file exists in the given location (1 Reply)
Discussion started by: claire.a
1 Replies

4. Shell Programming and Scripting

awk syntax error

Hi All, I wrote a simple script.sh program for i in seq (22) do awk '$1==${i}' file1.txt|awk '{print $2}'> file${i}_study.txt done and then run it %bash %chmod +x script.sh % ./script.sh Give me error awk: $1==${i} awk: ^ syntax error Do you have any idea why... (3 Replies)
Discussion started by: senayasma
3 Replies

5. Shell Programming and Scripting

Syntax error using Awk

more report2.txt how to modify the AWK to print above out put for given n no of inputs using report2.txt file? out put should be (3 Replies)
Discussion started by: kanakaraju
3 Replies

6. Shell Programming and Scripting

for loop not working - syntax error at line 6: `end of file' unexpected

I have a file called test.dat which contains a b I have written a shell script called test.sh for i in `cat test.dat` do echo $i done When i run this script using sh test.sh I get this message - test.sh: syntax error at line 6: `end of file' unexpected What is the... (3 Replies)
Discussion started by: debojyoty
3 Replies

7. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

8. UNIX for Dummies Questions & Answers

awk syntax error

I generate a fullpath variable two different ways: 1) concat two variables rootpath and relpath to give me fullpath 2) use 'find' to get the fullpath when I echo the variable, I get the same output for both. However, I print the variable from method 1 in the below loop I get "awk syntax... (0 Replies)
Discussion started by: orahi001
0 Replies

9. UNIX for Dummies Questions & Answers

'awk' syntax error

Due to some syntax error, my below code is not working. #!/usr/bin/ksh nawk ' BEGIN { cur_val=0; cur_zero=0; cur_nine=0; sum_zero=0; sum_nine=0; } /^/ { cur_val=substr($0,5,2); if("cur_val" == "0") { ... (3 Replies)
Discussion started by: lokiman
3 Replies

10. Shell Programming and Scripting

syntax error with awk.

A shell script a.sh calls an awk script using : awk -v OUTPUTDIR=${OUTPUTDIR}${OUTPUTDIRDATE} -f ${SCRIPTSPATH}chngNullBillId.awk ${INPUTFILE} chngNullBillId.awk : { if (substr($0,9,4)=="0000") printf( "%s0001%s", substr($0,1,8), substr($0,13,67) )>>${OUTPUTDIR}"goodfile.txt"; else print... (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies
Login or Register to Ask a Question