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.

# ------------------------------------------------------------------------
# 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

why do you need bc ?

sum=$(${awkcmd} '{asum += $1} END {printf ("%.4f\n", asum)}' temp.hash.txt)

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.

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

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

(standard_in) 3: parse error

which would be the line in red below:

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:

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

$
$ 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(?)

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

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

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.