arithmetic in bash
Hi,
I've a txt file that contains values in 2 columns: Code:
#field1 field2 |
If this is not homework, try a different tool than bash.
Code:
$ ruby -ane 'puts $F[0].to_f/$F[1].to_f' file |
This is not homework. I prefer in bash.
Here's the command that I use and it won't work. expr 1/(`cat tmp.txt | awk '{ print $1/$2 }'`) |
Well you just said in bash but are using awk?? As kurumi said, awk can do it on its own.
Code:
awk 'NR>1{print 1/(1 - $1/$2)}' file |
Code:
answer=$((1/(1-(field1/field2)))) http://tldp.org/LDP/abs/html/arithexp.html |
@arizonagroovejet - I do not see how your solution is of any use? All answers will be 1 as the fraction portion will always equate to 0 so it is just 1/1.
|
Quote:
|
Hi grail,
can you explain me what are you saying please? How the fraction part is always 0? |
All of the numbers in your file when column 1 is divided by column 2 will end up as a fraction less than 1. Bash does not actually round numbers to the nearest integer,
it actually truncates the decimal portion so all of your values will equate to 0. Hope that helps. |
Thanks, now I get it.
|
All times are GMT -5. The time now is 05:41 AM. |