home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9634 < prev    next >
Encoding:
Text File  |  1992-07-31  |  3.4 KB  |  108 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!casbah.acns.nwu.edu!navarra
  3. From: navarra@casbah.acns.nwu.edu (John Navarra)
  4. Subject: Using sed to convert numbers (interesting)
  5. Message-ID: <1992Jul31.121118.27783@news.acns.nwu.edu>
  6. Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
  7. Organization: Northwestern University, Evanston Illinois.
  8. Date: Fri, 31 Jul 1992 12:11:18 GMT
  9. Lines: 97
  10.  
  11. This is an interesting little problem I am working on. I am trying to
  12. convert numbers from scientific notation to regular (decimal) notation.
  13. I am using sed to do this. So far, this is what I have:
  14.  
  15. [casbah:411] ~/bin -> cat Etodec
  16. sed -n '
  17. s/\([0-9]\)\.\([0-9]\)\([0-9]*\)E+01/\1\2.\3/p
  18. s/\([0-9]\)\.\([0-9][0-9]\)\([0-9]*\)E+02/\1\2.\3/p
  19. s/\([0-9]\)\.\([0-9]*\)E-01/.\1\2/p
  20. s/\([0-9]\)\.\([0-9]*\)E-02/.0\1\2/p' $*
  21. [casbah:411] ~/bin ->
  22.  
  23. and I have the file with scientific numbers:
  24. [casbah:414] /tmp/navarra -> cat sci.numbers
  25. 1.2345E-01
  26. -1.2345E-01
  27. 1.2345E+01
  28. -1.2345E+01
  29. 1.2345E-02
  30. -1.2345E-02
  31. 1.2345E+02
  32. -1.2345E+02
  33.  
  34. Notice first of all that all the numbers are of the form:
  35. [-]X.XXXXXE[+-]XX
  36.  
  37. In particular, the fact that there is only one number before the decimal
  38. is crucial in the substitutions.
  39.  
  40. So far, Etodec (scientific 'E' to 'decimal') is working fine:
  41. [casbah:415] /tmp/navarra -> Etodec sci.numbers
  42. .12345
  43. -.12345
  44. 12.345
  45. -12.345
  46. .012345
  47. -.012345
  48. 123.45
  49. -123.45
  50.  
  51. However, just to spark some discussion on this, I have two questions.
  52.  
  53. 1) for you sed gurus, I don't understand WHY my substitutions are working
  54.    for negative numbers. For instance, the line 
  55.  
  56. s/\([0-9]\)\.\([0-9]*\)E-01/.\1\2/p
  57.  
  58.    looks for numbers of the form   '1.2345E-01' or '-1.2345E-01'. However,
  59.    I am confused as to how the hold buffer is working. The first thing
  60.    I look for is the first number before the decimal (placing it in the
  61.    first hold buffer), followed by a '.' Then the rest of the numbers
  62.    are placed in the second hold buffer. What I don't understand is
  63.    why the replacement pattern '.\1\2' doesn't produce something like:
  64.  
  65.    .-12345
  66.  
  67.    And similarly, for the case of E+01, why I don't get something like:
  68.  
  69.    1-2.345
  70.  
  71.    If I try the line:
  72. s/\([0-9]\)\.\([0-9]*\)E-01//p
  73.  
  74.    I get -'s on the matched lines.
  75.    How does sed know to put the '-' back at the beginning of the line?
  76.  
  77.  
  78. 2) Secondly, as you can probably tell, as the numbers get bigger or
  79.    smaller, the substitution lines will as well. I wrote the line
  80.    to limit the number of hold buffers to three for positive numbers
  81.    and two for negative numbers. However as the numbers get bigger
  82.    or smaller, here is what those lines will look like:
  83.  
  84. positive
  85. s/\([0-9]\)\.\([0-9]\)\([0-9]*\)E+01/\1\2.\3/p
  86. s/\([0-9]\)\.\([0-9][0-9]\)\([0-9]*\)E+02/\1\2.\3/p
  87. s/\([0-9]\)\.\([0-9][0-9][0-9]\)\([0-9]*\)E+03/\1\2.\3/p
  88. ...
  89. s/\([0-9]\)\.\([0-9]...[0-9]...[0-9][0-9]\)\([0-9]*\)E+XX/\1\2.\3/p
  90.  
  91. negative
  92. s/\([0-9]\)\.\([0-9]*\)E-01/.\1\2/p
  93. s/\([0-9]\)\.\([0-9]*\)E-02/.0\1\2/p'
  94. s/\([0-9]\)\.\([0-9]*\)E-03/.00\1\2/p'
  95. ...
  96. s/\([0-9]\)\.\([0-9]*\)E-XX/.0000.....0....\1\2/p'
  97.  
  98.    What I need is a variable which will scan what the value of 'XX' is
  99. in E[+-]XX and for numbers > 1 (E+XX) , write out 'XX' number of '[0-9]'
  100. strings in the pattern, and for numbers < 1 (E-XX), write out XX-1 zeroes
  101. in the replacement string. Any suggestions on how to do this?
  102.  
  103.     There is also one more problem I noticed for postive numbers
  104. which I will bring up if a discussion insues.
  105.  
  106. Any suggestions are appreciated,
  107. -tms
  108.