Results 1 to 10 of 16
Hi guys,
Having some trouble finding a method to perform a calculation between the contents of two files... For example
Say i have:
File 1
-------------
1
23
54343
123
...
- 05-09-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 18
EXPR the value of 2 files
Hi guys,
Having some trouble finding a method to perform a calculation between the contents of two files... For example
Say i have:
File 1
-------------
1
23
54343
123
45676
8855
-------------
File 2
-------------
9087
576
7897
4778
665
4545
-------------
I would like to add or divide the contents of the two files between each other, such as:
1*9087
23*576
54343*7897
123*4778
45676*665
8855*4545
and finally output the result to another file...
How am I able to add the contents of these files?
Note: The number of lines between each file is always the same...
Thanks for any help you can offer!!
Mark
- 05-09-2008 #2
With zsh:
This should work with other shells:Code:printf "%d\n" $(paste -d\* file1 file2)>final
Code:printf "%s\n" $(paste -d\* file1 file2)|bc>final
Or just use awk:
Or:Code:awk 'NR==FNR{_[FNR]=$0;next}$0=$0*_[FNR]' file1 file2>final
P.S. Post modified ...Code:eval printf "%s\\\n" $(printf '$((%s)) ' $(paste -d\* file1 file2))
- 05-09-2008 #3
I like the second alternative:
but it can be cleaned up thus:Code:printf "%s\n" $(paste -d\* file1 file2)|bc>final
Code:paste -d\* file1 file2 | bc -q > final
--
Bill
Old age and treachery will overcome youth and skill.
- 05-09-2008 #4
Absolutely right

I didn't realize that I was in zsh when I posted this:
And after that I tried to give more portable solution in a hurryCode:printf "%d\n" $(paste -d\* file1 file2)
- 05-09-2008 #5Just Joined!
- Join Date
- May 2008
- Posts
- 18
Thanks for the tips guys!!
I tried using the cleaned version, however i dont get any output back...
Im using Bash... and i get the following output:
# ./sed.sh
$ paste -d\* c.txt d.txt | bc -q > final
+ paste '-d*' c.txt d.txt
+ bc -q
Do you know what might be the cause of this?
Thanks!
- 05-09-2008 #6
- 05-09-2008 #7Just Joined!
- Join Date
- May 2008
- Posts
- 18
Perfect! worked like a charm...
Thanks m8!
- 05-09-2008 #8Just Joined!
- Join Date
- May 2008
- Posts
- 18
Hi again

So i needed to make a change, rather then a multiplication, i would need to perform a division... such as:
paste -d\/ file1 file2 | bc -q > final
However, the problem that the file final only uses whole numbers... however im performing a division on ms... such as 0.0000445..
Is there any way of getting a number to 10 decimal points?
Thanks!
- 05-09-2008 #9If you want a leading 0:Code:
bc<<! >final scale=10 $(paste -d\/ file1 file2) !
Code:printf "%.10f\n" $(paste -d\/ file1 file2|bc -l)>final
- 05-10-2008 #10Just Joined!
- Join Date
- May 2008
- Posts
- 18
Thanks once again... that did the trick...
Ok im nearly ready here... however encountered another issue...
Basically im parsing a document which contains SQL Statements that were run... calculating the number of times a SQL Statement was run agianst the total execution time...
However, when i encounter an issue were the SQL Statement times run is 0 and the total execution time is 0... i.e 0 / 0 generats the following error:
Runtime error (func=(main), adr=11): Divide by zero.
The error is generated by bc. Is there a method of handling this exception, therefore should the division be 0/0... You return the result 0?
Thanks once agani guys!


Reply With Quote