# Bash Decimal Addition using bc

**URL:** <https://community.unix.com/t/bash-decimal-addition-using-bc/278741>\
**Category:** Shell Programming and Scripting\
**Created:** [December 7, 2010, 4:16pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741 "2010-12-07T16:16:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vb615](https://community.unix.com/letter_avatar/vb615/32/5_5575768a8748004e209b776fc1b2916d.png) [@vb615](https://community.unix.com/u/vb615)\
**Post date:** [December 7, 2010, 4:16pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/1 "2010-12-07T16:16:40Z")

</div>

Hi guys/gals i have a quick question. I am trying to read from a file and add the values up but the problem is the values are not integers their floats so i tried to used bc but failed epicly lol. Any tips would be great. Thanks

this is the code i have so far :

```nohighlight
while read num
do
    total=$(echo "scale=2; $num + $total" | bc)
done < <(cat file.txt)

echo "Total: " $total

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/5_5575768a8748004e209b776fc1b2916d.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [December 7, 2010, 4:33pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/2 "2010-12-07T16:33:30Z")

</div>

Hi.

Can you post a sample of the input file?

```nohighlight
$ cat file1
1.1
3.2
5.9

$ total=$(paste -sd+ - < file1 | bc)

$ echo $total
10.2

```

---

<div class="post-metadata">

**Author:** ![vb615](https://community.unix.com/letter_avatar/vb615/32/5_5575768a8748004e209b776fc1b2916d.png) [@vb615](https://community.unix.com/u/vb615)\
**Post date:** [December 7, 2010, 4:42pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/3 "2010-12-07T16:42:55Z")

</div>

Here's one of the file i have to work with.

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [December 7, 2010, 4:55pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/4 "2010-12-07T16:55:40Z")

</div>

A glance at "file.txt" suggests that it needs some preprocessing.

There is a textual heading on line 1.  
There are numerous blank lines.

No problem with the numbers. They are consistently to two decimal places.

Because I had no problem reading the file with M$ "notepad" you will be converting the file to unix text file format?

---

<div class="post-metadata">

**Author:** ![vb615](https://community.unix.com/letter_avatar/vb615/32/5_5575768a8748004e209b776fc1b2916d.png) [@vb615](https://community.unix.com/u/vb615)\
**Post date:** [December 7, 2010, 5:41pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/5 "2010-12-07T17:41:00Z")

</div>

i figured the heading wouldn't affect the read in unless im mistaken but i could easily remove that. And the spaces could be taken out using sed '/^$/d' file.txt  
so im still having trouble with reading from the file and using bc. I keep getting parse error.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/5_5575768a8748004e209b776fc1b2916d.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [December 7, 2010, 5:53pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/6 "2010-12-07T17:53:30Z")

</div>

bc may be more trouble than it's worth here.

```nohighlight
$ awk '{T+=$0} END { printf "%.2f\n", T }' file1
63304750.57

```

---

<div class="post-metadata">

**Author:** ![vb615](https://community.unix.com/letter_avatar/vb615/32/5_5575768a8748004e209b776fc1b2916d.png) [@vb615](https://community.unix.com/u/vb615)\
**Post date:** [December 7, 2010, 6:25pm UTC](https://community.unix.com/t/bash-decimal-addition-using-bc/278741/7 "2010-12-07T18:25:46Z")

</div>

awesome that works. Thanks
