Archive for April, 2021

15
Apr

Bash convert number

   Posted by: admin    in Mẹo vặt của hiếu râu

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. 1.2345678e2=123.45678
  2. 1.2345678e-2=0.012345678
  3. 1.7615562e + 06=1761556.2
  4. 1.87982e7=18798200
  5. 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:

  1. [root@kevin ~] #echo "1.7615562e6" | gawk "$1=strtonum ($1)"
  2. 1.76156e + 06

1) Conversion of scientific notation to decimal

  1. [root@kevin ~] #printf "%f" 1.7615569e + 06
  2. 1761556.900000
  3. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}"
  4. 1761556.900000
  5. [root@kevin ~] #echo "1.7615569e + 03" | awk "{printf ("%f ", $0)}"
  6. 1761.556900
  7. [root@kevin ~] #echo "1.7615569e + 04" | awk "{printf ("%f ", $0)}"
  8. 17615.569000
  9. [root@kevin ~] #echo "1.7615569e-6" | awk "{printf ("%f ", $0)}"
  10. 0.000002
  11. [root@kevin ~] #echo "1.7615569e-4" | awk "{printf ("%f ", $0)}"
  12. 0.000176
  13. [root@kevin ~] #echo "1.7615569e-3" | awk "{printf ("%f ", $0)}"
  14. 0.001762
  15. [root@kevin ~] #echo "1.7615569e-2" | awk "{printf ("%f ", $0)}"
  16. 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

  1. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%. 2f ", $0)}"
  2. 1761556.90

Keep three decimal places

  1. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%. 3f ", $0)}"
  2. 1761556.900

3) Scientific notation is converted to decimal and rounded

  1. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%d ", $0)}"
  2. 1761556
  3. [root@kevin ~] #echo "1.7615569e3" | awk "{printf ("%d ", $0)}"
  4. 1761
  5. [root@kevin ~] #echo "1.7615569e02" | awk "{printf ("%d ", $0)}"
  6. 176

4) Scientific notation is converted to decimal and rounded

  1. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%d ", $0 + 0.5)}"
  2. 1761557
  3. [root@kevin ~] #echo "1.7615563e + 06" | awk "{printf ("%d ", $0 + 0.5)}"
  4. 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

  1. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}"
  2. 1761556.900000
  3. [root@kevin ~] #var=$(echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}")
  4. [root@kevin ~] #echo ${var %%0 *}
  5. 1761556.9

Or use the sed method

  1. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}"
  2. 1761556.900000
  3. [root@kevin ~] #var=$(echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}")
  4. [root@kevin ~] #echo "$var" | sed "s/0 * $//"
  5. 1761556.9
  6. [root@kevin ~] #echo "1.7615569e + 06" | awk "{printf ("%f ", $0)}" | sed "s/0 * $//"
  7. 1761556.9

6) Sed removes the characters after the decimal point

  1. [root@kevin ~] #echo "kevin.123" | sed "s /\.*//g"
  2. kevin123
  3. \ .. * represents 1 or more after the decimal point
  4. [root@kevin ~] #echo "kevin.123" | sed "s /\..*//g"
  5. kevin
  6. [root@kevin ~] #echo "kevin_123" | sed "s/\ _ * //g"
  7. kevin123
  8. [root@kevin ~] #echo "kevin_123" | sed "s /\_.*//g"
  9. 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. 1) bc method
  2. bc is a more commonly used linux computing tool,And supports floating-point operations:
  3. [root@kevin ~] #a=`echo 1 + 1 | bc`
  4. [root@kevin ~] #echo $a
  5. 2
  6. This method cannot solve the precision problem of floating point arithmetic,The following floating point calculations are silly x!
  7. [root@kevin ~] #a=`echo 1 + 1 | bc`
  8. [root@kevin ~] #echo $a
  9. 2
  10. [root@kevin ~] #b=`echo" 1.2 * 1.2 "| bc`
  11. [root@kevin ~] #echo $b
  12. 1.4
  13. [root@kevin ~] #c=`echo" 5.0/3.0 "| bc`
  14. [root@kevin ~] #echo $c
  15. 1
  16. [root@kevin ~] #d=`echo" scale=2;5.0/3.0 "| bc`
  17. [root@kevin ~] #echo $d
  18. 1.66
  19. [root@kevin ~] #e=`echo" scale=2;5.0/6.0 "| bc`
  20. [root@kevin ~] #echo $e
  21. .83
  22. 2) expr method
  23. Does not support floating point calculations,This is a pit,And pay attention to the spaces in numbers and operators.
  24. [root@kevin ~] #a=`expr 1 + 1`
  25. [root@kevin ~] #echo $a
  26. 1 + 1
  27. [root@kevin ~] #a=`expr 1 + 1`
  28. [root@kevin ~] #echo $a
  29. 2
  30. [root@kevin ~] #b=`expr 10/2`
  31. [root@kevin ~] #echo $b
  32. 5
  33. 3) $(()) method
  34. Same as expr, does not support floating point arithmetic
  35. [root@kevin ~] #a=$((1 + 1))
  36. [root@kevin ~] #echo $a
  37. 2
  38. [root@kevin ~] #b=$((1 + 3))
  39. [root@kevin ~] #echo $b
  40. 4
  41. 4) let method
  42. Does not support floating point arithmetic,And does not support direct output,Can only be assigned
  43. [root@kevin ~] #let a=1 + 1
  44. [root@kevin ~] #echo $a
  45. 2
  46. [root@kevin ~] #let b=50/5
  47. [root@kevin ~] #echo $b
  48. 10
  49. [root@kevin ~] #let c=1.2 * 2
  50. -bash:let:c=1.2 * 2:syntax error:invalid arithmetic operator (error token is ".2 * 2")
  51. 5) Awk method
  52. Common operations:
  53. [root@kevin ~] #a=`echo | awk" {print 1.0/2.0} "`
  54. [root@kevin ~] #echo $a
  55. 0.5
  56. control precision:
  57. [root@kevin ~] #b=`echo | awk" {printf ("%. 2f", 1.0/2.0)} "`
  58. [root@kevin ~] #echo $b
  59. 0.50
  60. Passing parameters:
  61. [root@kevin ~] #c=`echo | awk -v a=1 -v b=3" {printf ("%. 4f", a/b)} "`
  62. [root@kevin ~] #echo $c
  63. 0.3333
  64. Awk combined with begin (retain 6 digits after the decimal point)
  65. [root@ss-server ~] #awk "begin {printf"%.2f %%\ n ", (87/500) * 100}"
  66. 17.40%
  67. [root@ss-server ~] #awk "begin {printf"%.2f %%\ n ", (100/300) * 100}"
  68. 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.