# problem while adding column values in awk

**URL:** <https://community.unix.com/t/problem-while-adding-column-values-in-awk/245606>\
**Category:** Shell Programming and Scripting\
**Created:** [July 27, 2009, 7:07am UTC](https://community.unix.com/t/problem-while-adding-column-values-in-awk/245606 "2009-07-27T07:07:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![valokv](https://community.unix.com/letter_avatar/valokv/32/6_22734026db12803ebb3e059622ac3534.png) [@valokv](https://community.unix.com/u/valokv)\
**Post date:** [July 27, 2009, 7:07am UTC](https://community.unix.com/t/problem-while-adding-column-values-in-awk/245606/1 "2009-07-27T07:07:30Z")

</div>

Hi,

I have a file "input.txt" with the following content :

5312,0,,,1,8,141.2,20090727  
3714,0,,,1,8,285.87,20090727  
5426,0,,,1,8,3.9,20090727  
3871,0,,,1,8,30.4,20090727  
9071,0,,,1,8,146.2,20090727  
5141,0,,,1,8,2.8,20090727  
0460,0,,,1,8,-0.1,20090727  
7918,0,,,1,8,-0.1,20090727  
1887,0,,,1,8,2,20090727  
2083,0,,,1,8,22.8,20090727  
6766,0,,,1,8,89.6,20090727  
6001,0,,,1,8,2.3,20090727  
9152,0,,,1,8,84.7,20090727  
8138,0,,,1,8,31.9,20090727  
1571,0,,,1,8,29.4,20090727  
9730,0,,,1,8,408.7,20090727  
8751,0,,,1,8,22.2,20090727  
9015,0,,,1,8,983.7,20090727  
6358,0,,,1,8,6.3,20090727

The 7th field contains both +ve and -ve values. I want to get the resultant sum all the values in the 7th field. I used the following awk command :

awk -F"," '{print $7}' input.txt | awk '{sum+=$1} END {print sum}'

I am getting this result : 5.29935e+06

How can i convert the result to a proper decimal value because i need the resultant value to be passed into another script as input.

---------- Post updated at 11:07 AM ---------- Previous update was at 11:05 AM ----------

Forgot to mention that the file "input.txt" contains about 40000 rows and the 7th field might have small values like 0.02 or 0.001 etc.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [July 27, 2009, 7:09am UTC](https://community.unix.com/t/problem-while-adding-column-values-in-awk/245606/2 "2009-07-27T07:09:24Z")

</div>

Hi.

Multiply the result by 1.

i.e

```nohighlight
END {print sum*1}

```

Or use printf: i.e.

```nohighlight
END {printf( "%f\n", sum)}

```

Cheers,

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 27, 2009, 7:19am UTC](https://community.unix.com/t/problem-while-adding-column-values-in-awk/245606/3 "2009-07-27T07:19:53Z")

</div>

One awk command is sufficient:

```nohighlight
awk -F, '{s+=$7}END{print s}' file

```

---

<div class="post-metadata">

**Author:** ![valokv](https://community.unix.com/letter_avatar/valokv/32/6_22734026db12803ebb3e059622ac3534.png) [@valokv](https://community.unix.com/u/valokv)\
**Post date:** [July 27, 2009, 8:03am UTC](https://community.unix.com/t/problem-while-adding-column-values-in-awk/245606/4 "2009-07-27T08:03:06Z")

</div>

Thanks Scottn and Franklin,

That printf command worked and now i am getting the proper value :

# awk -F, '{s+=$7}END{printf( "%f\n", s)}' input.txt

5299354.810000

Thanks for the help guys !
