|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Bash Scientific Notation
Hello there,
I have a script that must be written in bash that has to deal with reading in values from a file (in scientific notation), and requires executing some mathematical operations with them. What is the easiest way to go about doing this/converting it to float to use | bc, etc.? Thanks! |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
This should be a basic example: Code:
a=65; b=1.11; c=30 ; echo "scale= $a; $b ^ $c" | bc For anything else try to elaborate your request. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I think the main problem here is the "scientific notation". I guess you mean numbers like 1.234e29 which are not supported by bc either? Maybe something like this ...? Code:
echo "1.234e23 9.876e14" | sed -e 's/ /*/' | perl -nle 'print eval $_' Concur with danmero, an example of the input and expected output would help. |
|
#4
|
|||
|
|||
|
bc can do the job Code:
echo "1.234e23 9.876e14" | sed 's/e/*10^/g;s/ /*/' | bc |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Good catch; thanks for the correction.
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Or just awk: Code:
awk -v a="1.234e23" -v b="9.876e14" 'BEGIN{print (a * b)}' |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Thanks all, in case you are interested -- I ended up using sed:
floatVar1=$(echo $Var1 | sed 's/\([0-9]*\(\.[0-9]*\)\?\)[eE]+\?\(-\?[0-9]*\)/(\1*10^\3)/g;s/^/scale=30;/'| bc) There are some difficulties with the specific scientific notation my data files were using. (having an extra + after the E, 1.00e+1 = 10), so I had trouble getting bc to work, but I may have just messed up. However, the idea behind the question was supposed to be specifically how to convert something from scientific notation to float. Sorry for the confusion. |
| Sponsored Links | ||
|
![]() |
| Tags |
| bash scientific notation |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading Scientific notation from file and storing in array | Filter500 | Programming | 3 | 12-16-2011 02:45 PM |
| Rounding scientific notation | ppat7046 | Shell Programming and Scripting | 4 | 06-10-2009 02:20 PM |
| Conversion of scientific notation | gingburg | UNIX for Dummies Questions & Answers | 4 | 02-24-2009 01:45 PM |
| How to add/multiply numbers with scientific notation (2.343e-5) | chugger06 | UNIX for Dummies Questions & Answers | 1 | 01-26-2006 09:03 PM |
| How to Convert scientific notation to normal ? | maheshsri | Shell Programming and Scripting | 2 | 01-05-2006 12:33 PM |
|
|