home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / question / 14925 < prev    next >
Encoding:
Internet Message Format  |  1992-12-23  |  4.0 KB

  1. Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!nsisrv!Pt!postmaster@hq.af.mil!lpeters
  2. From: lpeters@postmaster@hq.af.mil (Leslie D Peters)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Performing simple math on a numeric file from csh
  5. Message-ID: <15420@hq.hq.af.mil>
  6. Date: 23 Dec 92 14:01:31 GMT
  7. References: <1992Dec16.080343.24756@netcom.com> <1992Dec17.093546.9935@netcom.com> <1992Dec22.002039.28769@Celestial.COM>
  8. Sender: news@Pt.hq.af.mil
  9. Reply-To: lpeters@marge.hq.af.mil
  10. Organization: 7th Communications Group
  11. Lines: 87
  12.  
  13. In article <1992Dec22.002039.28769@Celestial.COM>, ray@Celestial.COM (Ray Jones) writes:
  14. |> In <1992Dec17.093546.9935@netcom.com> dman@netcom.com (Dallman Ross) writes:
  15. |> 
  16. |> >I, Dallman Ross (dman@netcom.com), wrote:
  17. |> >: I am trying to understand awk, but the man pages are driving me nuts. 
  18. |> >: Anyway, I have a couple of simple files with numerics in them that I
  19. |> >: want to do some math with.  This doesn't have to be via awk, but I've
  20. |> >: been told that's the ticket.  Anyway, I use csh.
  21. |> >:  
  22. |> >: Here's what one of the files looks like:
  23. |> >:  
  24. |> >: > login:  Dec 15 21:40:01
  25. |> >: > logout: Dec 15 21:40:18
  26. |> >: > 
  27. |> >: > login:  Dec 15 21:41:11
  28. |> >: > logout: Dec 15 21:41:48
  29. |> >: > 
  30. |> >: > login:  Dec 15 22:20:08
  31. |> >: > logout: Dec 15 22:31:44
  32. |> >: > 
  33. |> >:  
  34. |> >: (No >'s in the file, of course.)  So, I want to subtract the
  35. |> >: penultimate time figure from the last figure, and come up with
  36. |> >: "00:11:36" as the answer for time logged in.  I only need to do the
  37. |> >: math on the last pair.  The rest is just a running log.
  38. |> >:  
  39. |> >: Thanks for any advice.
  40. |> 
  41. |> >Still hoping for some help on this.  One person did email me, but I
  42. |> >haven't seen any tips on command lines yet.
  43. |> 
  44. |> I think awk may be the easier way to go, but I don't know much about it
  45. |> either.  I would use a bourne shell script with something like the
  46. |> following:
  47. |> 
  48. |> tail -2 logfile>tmp.$$    # get the last entries
  49. |> set `grep login tmp.$$|sed -e "s/:/ /"`    # where 
  50. |> hin=$4     #hour of login
  51. |> min=$5     #minute of login
  52. |> sin=$6     #second of login
  53. |> set `grep logout tmp.$$|sed -e "s/:/ /"`    # where
  54. |> hout=$4     #hour of logout
  55. |> mout=$5     #minute of logout
  56. |> sout=$6     #second of logout
  57. |> # now you can use "bc" to do all the numbers
  58. |> t_hr=`echo "$hout-$hin" |bc`
  59. |> t_min=`echo "$mout-$min" |bc`
  60. |> t_sec=`echo "$sout-$sin" |bc`
  61. |> echo "login hours = $t_hr"
  62. |> echo "login minutes = $t_min"
  63. |> echo "login seconds = $t_sec"
  64. |> rm tmp.$$
  65. |> # crude, but it should work.
  66. |> -- 
  67. |> INTERNET:  ray@Celestial.COM   Ray A. Jones; Celestial Software
  68. |> UUCP:   ...!thebes!camco!ray   6641 East Mercer Way
  69. |>              uunet!camco!ray   Mercer Island, WA 98040; (206) 947-5591
  70. |> The probability of one or more spelling errors in this missive approaches
  71.  
  72. Nice, but it doesn't account for borrowing across hours, minutes, and 
  73. seconds.  Before the echo's, insert:
  74.  
  75. if [ $t_sec -lt 0 ]
  76. then
  77.    t_sec=`echo $t_sec+60|bc`
  78.    t_min=`echo $t_min-1|bc`
  79. fi
  80. if [ $t_min -lt 0 ]
  81. then
  82.    t_min=`echo $t_min+60|bc`
  83.    t_hr=`echo $t_hr-1|bc`
  84. fi
  85.  
  86. That'll fix it up.
  87. -- 
  88. /============================================================================\
  89. |                                            |                               |
  90. |                    ___                     |                               |
  91. |       ___....-----'---`-----....___        |   I haven't lost my mind --   |
  92. | =========================================  |                               |
  93. |        ___`---..._______...---'___         |    it's backed up on          |
  94. |       (___)      _|_|_|_      (___)        |           tape somewhere.     |
  95. |         \\____.-'_.---._`-.____//          |                               |
  96. |           ~~~~`.__`---'__.'~~~~            |   lpeters@marge.hq.af.mil     |
  97. |                   `~~~'                    |                               |
  98. |                                            |     Les Peters                |
  99. \============================================================================/
  100.