Hi,
i want to caluculate sum and absolute sum information of the 2nd column.
sum(abs(ENO)),sum(ENO)
file1 contains the employee information.
"abc","+10000.00","100"
"bbc","-3000.00","400"
"cbc","+20000.90","500"
"dbc","-4000.00","600"
output should get this
Sum(abs(eno)) sum(eno)
----------- -----------
37000.90 23000.90
i am getting sun value but i am unable to get the information on the sum of abs values.
Any help greatly appriciated.
Thanks
Please, show your code.
Here are the detailed information.
Sample data from the file_name.txt:
-30.1932871983700
-40.1149000000
+60.2300
+20.0012389018230123
-
-
so on
awk '{ Val += $1; AbsVal += ($1>=0?$1:(0-$1)) } END {printf("Value = %d\t Abs Value = %d\n", Val, AbsVal)}' file_name.txt
Command output: Sum and abs sum values are not coming properly.
Value = 18426854789.828552 Abs Value = 120707557235749.953125
i should get the below values.
Value =18426854789.85 Abs Value = 120707557235749.96
based on the fours i have tried this command but decimal values are not coming properly.
any help greatly appriciated.
thanks
nawk -F, -v qq='"' '{gsub(qq,"");s+=$2;sa+=($2<0)?-$2:$2}END{print sa, s}' OFMT='%.2f' myFile
sa+=($2<0)?-$2:$2
could also be
sa+=sqrt($2^2)
2 Likes
Just another smart way to solve a problem