LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2011, 11:07 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
arithmetic in bash


Hi,

I've a txt file that contains values in 2 columns:
Code:
#field1  field2
324        2343
232        13434
213        453
54         231
I would like to apply the equation 1/(1-(field1/field2)) in bash. How can I do that?

Last edited by xeon123; 04-22-2011 at 11:11 AM.
 
Old 04-22-2011, 11:11 AM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
If this is not homework, try a different tool than bash.
Code:
$ ruby -ane 'puts $F[0].to_f/$F[1].to_f' file
you can use also awk,Perl, Python etc that can support decimals
 
Old 04-22-2011, 11:15 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
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 }'`)
 
Old 04-22-2011, 11:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
Bash does not do fractions so you will at least need to use bc.
 
Old 04-22-2011, 01:12 PM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
Code:
answer=$((1/(1-(field1/field2))))
answer will be rounded to the nearest integer.

http://tldp.org/LDP/abs/html/arithexp.html
 
Old 04-23-2011, 01:18 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@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.
 
Old 04-23-2011, 04:12 AM   #7
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by grail View Post
All answers will be 1 as the fraction portion will always equate to 0 so it is just 1/1.
Yeah... good point. What I posted works and meets the OP's original stated requirement/desire of doing it in bash but you're right, it isn't of any practical use in this case given the numbers being used.
 
Old 04-23-2011, 09:07 AM   #8
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Hi grail,

can you explain me what are you saying please? How the fraction part is always 0?
 
Old 04-23-2011, 09:24 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 04-23-2011, 09:39 AM   #10
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Thanks, now I get it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] arithmetic operation in bash xeon123 Linux - Newbie 6 03-10-2011 07:12 PM
[SOLVED] bash arithmetic question ghantauke Linux - Newbie 4 11-15-2010 04:47 PM
BASH, simple arithmetic helptonewbie Programming 9 01-10-2007 05:53 PM
Bash arithmetic Blackout_08 Programming 2 06-08-2006 10:37 PM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 02:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration