# equation calculation on Unix

**URL:** <https://community.unix.com/t/equation-calculation-on-unix/166909>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [November 16, 2006, 6:42am UTC](https://community.unix.com/t/equation-calculation-on-unix/166909 "2006-11-16T06:42:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbinGlasses](https://community.unix.com/letter_avatar/robbinglasses/32/5_5575768a8748004e209b776fc1b2916d.png) [@robbinGlasses](https://community.unix.com/u/robbinGlasses)\
**Post date:** [November 16, 2006, 6:42am UTC](https://community.unix.com/t/equation-calculation-on-unix/166909/1 "2006-11-16T06:42:59Z")

</div>

Hay, guys,  
Any ideas how to calculate like this:  
in first file, there're number of lines listing 2+3, 6\*9 .......  
Then, how to get the result and put them in another file in format:

2+3 5  
6\*9 54

......

sheerly by shell command, no scripts required.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [November 16, 2006, 7:56am UTC](https://community.unix.com/t/equation-calculation-on-unix/166909/2 "2006-11-16T07:56:04Z")

</div>

sed 's/\*/\\\*/' file1 \> dummy  
sed 's/\//\\\//' dummy \> file2  
while read line  
do  
echo "$line \c" \>\> Output  
echo "$line" | bc \>\> Output  
done \< file2

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 16, 2006, 11:13am UTC](https://community.unix.com/t/equation-calculation-on-unix/166909/3 "2006-11-16T11:13:57Z")

</div>

```nohighlight
#!/bin/ksh

while read line
do
  echo "${line} $(echo ${line} | bc)"
done < myFile.txt

```

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [November 16, 2006, 11:37am UTC](https://community.unix.com/t/equation-calculation-on-unix/166909/4 "2006-11-16T11:37:36Z")

</div>

The way the question is phrased I tend to think that we may have homework rule violation here. :eek:  
But:  
bc \< input \> temp ; paste input temp \> output ; rm temp

---

<div class="post-metadata">

**Author:** ![robbinGlasses](https://community.unix.com/letter_avatar/robbinglasses/32/5_5575768a8748004e209b776fc1b2916d.png) [@robbinGlasses](https://community.unix.com/u/robbinGlasses)\
**Post date:** [November 16, 2006, 6:13pm UTC](https://community.unix.com/t/equation-calculation-on-unix/166909/5 "2006-11-16T18:13:04Z")

</div>

Thanks Perderabo,  
I think you answer is fairly enough. vgersh99&justsam's genius work is also appreciated.
