Archive for April, 2021
Bash convert number
Method of converting scientific notation (e) to numbers under Linux (tutorialfor.com)
Scientific notation uses e to identify values,The idea of turning scientific computing into numbers:press the number to the right of e to move the number of decimal places.If the number to the right of e is negative,Then move the decimal point to the left.Examples are as follows:
1.2345678e2=123.45678
1.2345678e-2=0.012345678
1.7615562e + 06=1761556.2
1.87982e7=18798200
1e3=1000
So how do you convert scientific notation into numbers in the shell,The method is as follows:Here take “1.7615562e + 06″ (or 1.7615562e6) as an example:
[root@kevin ~] #echo "1.7615562e6" | gawk "$1=strtonum ($1)"
1.76156e + 06
1) Conversion of scientific notation to decimal
[root@kevin ~] #printf "%f" 1.7615569e + 06
1761556.900000
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}"
1761556.900000
[root@kevin ~] #echo "1.7615569e + 03" | awk "{printf ("%f ", $0)}"
1761.556900
[root@kevin ~] #echo "1.7615569e + 04" | awk "{printf ("%f ", $0)}"
17615.569000
[root@kevin ~] #echo "1.7615569e-6" | awk "{printf ("%f ", $0)}"
0.000002
[root@kevin ~] #echo "1.7615569e-4" | awk "{printf ("%f ", $0)}"
0.000176
[root@kevin ~] #echo "1.7615569e-3" | awk "{printf ("%f ", $0)}"
0.001762
[root@kevin ~] #echo "1.7615569e-2" | awk "{printf ("%f ", $0)}"
0.017616
Note:This method,In the conversion result, 6 digits are reserved after the decimal point:
1) If the number after e is positive,6 digits should be reserved after the decimal point, if not enough,Make up with 0.
2) If the number after e is negative,6 digits should be reserved after the decimal point.6 bits are reserved.
2) Convert scientific notation to decimal and retain two decimal places
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%. 2f ", $0)}"
1761556.90
Keep three decimal places
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%. 3f ", $0)}"
1761556.900
3) Scientific notation is converted to decimal and rounded
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%d ", $0)}"
1761556
[root@kevin ~] #echo "1.7615569e3" | awk "{printf ("%d ", $0)}"
1761
[root@kevin ~] #echo "1.7615569e02" | awk "{printf ("%d ", $0)}"
176
4) Scientific notation is converted to decimal and rounded
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%d ", $0 + 0.5)}"
1761557
[root@kevin ~] #echo "1.7615563e + 06" | awk "{printf ("%d ", $0 + 0.5)}"
1761556
5) How to remove the useless 0 after the decimal point, you can refer to:daily operation and maintenanceIntercept string in shell scriptPractice, that is, using variable expansion
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}"
1761556.900000
[root@kevin ~] #var=$(echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}")
[root@kevin ~] #echo ${var %%0 *}
1761556.9
Or use the sed method
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}"
1761556.900000
[root@kevin ~] #var=$(echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}")
[root@kevin ~] #echo "$var" | sed "s/0 * $//"
1761556.9
[root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}" | sed "s/0 * $//"
1761556.9
6) Sed removes the characters after the decimal point
[root@kevin ~] #echo "kevin.123" | sed "s /\.*//g"
kevin123
\ .. * represents 1 or more after the decimal point
[root@kevin ~] #echo "kevin.123" | sed "s /\..*//g"
kevin
[root@kevin ~] #echo "kevin_123" | sed "s/\ _ * //g"
kevin123
[root@kevin ~] #echo "kevin_123" | sed "s /\_.*//g"
kevin
Command explanation:
* Represents 0 to more. Therefore, \. * Can only replace the decimal point and become empty.\ _ * Similarly.
. * Stands for 1 or more. Therefore, \ .. * removed everything after the decimal point.\ _. * Similarly.
7) Description of numerical calculation in the shell
1) bc method
bc is a more commonly used linux computing tool,And supports floating-point operations:
[root@kevin ~] #a=`echo 1 + 1 | bc`
[root@kevin ~] #echo $a
2
This method cannot solve the precision problem of floating point arithmetic,The following floating point calculations are silly x!
[root@kevin ~] #a=`echo 1 + 1 | bc`
[root@kevin ~] #echo $a
2
[root@kevin ~] #b=`echo" 1.2 * 1.2 "| bc`
[root@kevin ~] #echo $b
1.4
[root@kevin ~] #c=`echo" 5.0/3.0 "| bc`
[root@kevin ~] #echo $c
1
[root@kevin ~] #d=`echo" scale=2;5.0/3.0 "| bc`
[root@kevin ~] #echo $d
1.66
[root@kevin ~] #e=`echo" scale=2;5.0/6.0 "| bc`
[root@kevin ~] #echo $e
.83
2) expr method
Does not support floating point calculations,This is a pit,And pay attention to the spaces in numbers and operators.
[root@kevin ~] #a=`expr 1 + 1`
[root@kevin ~] #echo $a
1 + 1
[root@kevin ~] #a=`expr 1 + 1`
[root@kevin ~] #echo $a
2
[root@kevin ~] #b=`expr 10/2`
[root@kevin ~] #echo $b
5
3) $(()) method
Same as expr, does not support floating point arithmetic
[root@kevin ~] #a=$((1 + 1))
[root@kevin ~] #echo $a
2
[root@kevin ~] #b=$((1 + 3))
[root@kevin ~] #echo $b
4
4) let method
Does not support floating point arithmetic,And does not support direct output,Can only be assigned
[root@kevin ~] #let a=1 + 1
[root@kevin ~] #echo $a
2
[root@kevin ~] #let b=50/5
[root@kevin ~] #echo $b
10
[root@kevin ~] #let c=1.2 * 2
-bash:let:c=1.2 * 2:syntax error:invalid arithmetic operator (error token is ".2 * 2")
5) Awk method
Common operations:
[root@kevin ~] #a=`echo | awk" {print 1.0/2.0} "`
[root@kevin ~] #echo $a
0.5
control precision:
[root@kevin ~] #b=`echo | awk" {printf ("%. 2f", 1.0/2.0)} "`
[root@kevin ~] #echo $b
0.50
Passing parameters:
[root@kevin ~] #c=`echo | awk -v a=1 -v b=3" {printf ("%. 4f", a/b)} "`
[root@kevin ~] #echo $c
0.3333
Awk combined with begin (retain 6 digits after the decimal point)
[root@ss-server ~] #awk "begin {printf"%.2f %%\ n ", (87/500) * 100}"
17.40%
[root@ss-server ~] #awk "begin {printf"%.2f %%\ n ", (100/300) * 100}"
33.33%
in summary,Or awk’s method is the most reliable,Other methods have corresponding problems.Therefore, it is recommended to use awk to perform mathematical calculations in daily maintenance scenarios.