1

In bash I am trying to code a conditional with numbers that are decimals (with fractions). Then I found out that I cannot do decimals in bash. The script that I have is as follows:

 a=$(awk '/average TM cross section = / {CCS=$6}; END {printf "%15.4E \n",CCS}' ${names}_$i.out) a=$(printf '%.2f\n' $a) echo $a 

In the *.out file the numbers are in scientific-notation. At the end the echo $a results me in a number 245.35 (or other numbers in my files). So, I was wondering how to change the out put number 245.35 in to 24535 so I can do a conditional in bash.

I tried to multiply and that obviously did not work. Can anyone help with this conversion?

7
  • Please edit your question to indicate if you need rounding, ie 245.35 = 245 VS 245.99 = 246? Do you need to work with scientific-notation, i.e. 1.1457e6 ? Good luck. Commented Apr 30, 2012 at 21:43
  • I don't need any rounding at all. the number 245.35 is coming from scientific notation 2.4535E2. Commented Apr 30, 2012 at 21:47
  • please edit your question to include sample input and required output. That is, does your data every contain 2.4535E2? Otherwise we're just guessing what you need. Good luck. Commented Apr 30, 2012 at 21:48
  • why all the extra steps, why not just ...END { printf("%15d\n", CCS) }'... Good luck. Commented Apr 30, 2012 at 22:25
  • doing that just gives me 3 numbers. 245 Commented Apr 30, 2012 at 22:32

3 Answers 3

1

You might do best to use something other than bash for your arithmetic -- call out to something with a bit more power. You might find the following links either inspiring or horrifying: http://blog.plover.com/prog/bash-expr.html ("Arithmetic expressions in shell scripts") and http://blog.plover.com/prog/spark.html ("Insane calculations in bash"); I'm afraid this is the sort of thing you're liable to end up with if you seriously try to do bash-based arithmetic. In particular, the to_rational function in the second of those articles includes some code for splitting up decimals using regular expressions, though he's doing something more complicated with them than it sounds like you do.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll say horrifying. I have to say that I cannot understand with my very shallow knowledge on scripting.
Unfortunately, the horror is pretty much unavoidable. If you have any alternative to doing arithmetic in bash, you should probably take it...
1

Per our extended conversation

 a=$(awk '/average TM cross section = / {CCS=$6}; END {printf "%15d\n",CCS * 100}' ${names}_$i.out) 

Now your output will be an integer.

Note that awk is well designed for processing large files and testing logic. It is likely that your all/most of your processing could be done in one awk process. If you're processing large amounts of data, the time savings can be significant.

I hope this helps.

Comments

0

as per the info provided by you , this is not related to any arithmetic operation.

treat it as string . find decimal point and remove it . that's what i understand

http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

http://www.thegeekstuff.com/2010/07/bash-string-manipulation/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.