Hello
first, truth been told, I'm not even close to be advanced user. I'm posting here because maybe my question is complicated enough to need your expert help
I need to use awk (or nawk - I don't have gawk) to validate some files by computing the total sum for a large numeric variable.
It can be that my OS does not support it or it can be as simply as modify my command
in this forum I found a simpler command, but that can indicates a limitation
/da9$ echo "12345678910 12345678910" | awk '{sum=$1; sum+=$2; print sum}'
2.46914e+10
/da9$ echo "1234567891 1234567891" | awk '{sum=$1; sum+=$2; print sum}'
2.46914e+09
/da9$ echo "123456789 123456789" | awk '{sum=$1; sum+=$2; print sum}'
246913578
/da9$ file `which awk`
/usr/bin/awk: executable (RISC System/6000) or object module
$ uname -s -r -v
AIX 1 6
$ uname -a
AIX blabla 1 6 00C608324C00
can someone help me make this sum or point me any configuration requirements?
in reality, this is the command I want to use (it can be that the inner print is causing the e+ notation, but I've tried printf and got even worse results):
cat a01.txt | nawk -F"\t" '{print $7,$8,$2";"$18}'| sort | nawk -F";" 'BEGIN{var="inicio";soma=0};{if(var!=$1) {if(var!="inicio") {print var,soma ; var=$1 ; soma=$2} else {var=$1;soma=$2}} else {var=$1;soma=soma+$2} };END{printf "\n",var,"%17.2f",soma}'
(output)
CashB Aprovado S-01 1533.44
Compr Aprovado S-01 3.23697e+09
---------- Post updated at 03:11 PM ---------- Previous update was at 02:45 PM ----------
SOLVED!!
credits to my wonderful friend Luiz Germano (who even called me at a Saturday!)
the problem really was the inner print and the solution was to format both variables
cat a01.txt | nawk -F"\t" '{print $7,$8,$2";"$18}'| sort | nawk -F";" 'BEGIN{var="inicio";soma=0};{if(var!=$1) {if(var!="inicio") {printf ("%s %17.2f\n",var,soma) ; var=$1 ; soma=$2} else {var=$1;soma=$2}} else {var=$1;soma=soma+$2} };END{printf "%s %17.2f\n",var,soma}'
you can use this as a sumif on excel - sum one column by the values of another column - sumif with awk