From: lpeters@postmaster@hq.af.mil (Leslie D Peters)
Newsgroups: comp.unix.questions
Subject: Re: Performing simple math on a numeric file from csh
Date: 23 Dec 92 14:01:31 GMT
Reply-To: lpeters@marge.hq.af.mil
Organization: 7th Communications Group

In article <1992Dec22.002039.28769@Celestial.COM>, ray@Celestial.COM (Ray Jones) writes:
|> In <1992Dec17.093546.9935@netcom.com> dman@netcom.com (Dallman Ross) writes:
|> 
|> >I, Dallman Ross (dman@netcom.com), wrote:
|> >: I am trying to understand awk, but the man pages are driving me nuts. 
|> >: Anyway, I have a couple of simple files with numerics in them that I
|> >: want to do some math with.  This doesn't have to be via awk, but I've
|> >: been told that's the ticket.  Anyway, I use csh.
|> >:  
|> >: Here's what one of the files looks like:
|> >:  
|> >: > login:  Dec 15 21:40:01
|> >: > logout: Dec 15 21:40:18
|> >: > 
|> >: > login:  Dec 15 21:41:11
|> >: > logout: Dec 15 21:41:48
|> >: > 
|> >: > login:  Dec 15 22:20:08
|> >: > logout: Dec 15 22:31:44
|> >: > 
|> >:  
|> >: (No >'s in the file, of course.)  So, I want to subtract the
|> >: penultimate time figure from the last figure, and come up with
|> >: "00:11:36" as the answer for time logged in.  I only need to do the
|> >: math on the last pair.  The rest is just a running log.
|> >:  
|> >: Thanks for any advice.
|> 
|> >Still hoping for some help on this.  One person did email me, but I
|> >haven't seen any tips on command lines yet.
|> 
|> I think awk may be the easier way to go, but I don't know much about it
|> either.  I would use a bourne shell script with something like the
|> following:
|> 
|> tail -2 logfile>tmp.$$	# get the last entries
|> set `grep login tmp.$$|sed -e "s/:/ /"`	# where 
|> hin=$4 	#hour of login
|> min=$5 	#minute of login
|> sin=$6 	#second of login
|> set `grep logout tmp.$$|sed -e "s/:/ /"`	# where
|> hout=$4 	#hour of logout
|> mout=$5 	#minute of logout
|> sout=$6 	#second of logout
|> # now you can use "bc" to do all the numbers
|> t_hr=`echo "$hout-$hin" |bc`
|> t_min=`echo "$mout-$min" |bc`
|> t_sec=`echo "$sout-$sin" |bc`
|> echo "login hours = $t_hr"
|> echo "login minutes = $t_min"
|> echo "login seconds = $t_sec"
|> rm tmp.$$
|> # crude, but it should work.
|> -- 
|> INTERNET:  ray@Celestial.COM   Ray A. Jones; Celestial Software
|> UUCP:   ...!thebes!camco!ray   6641 East Mercer Way
|>              uunet!camco!ray   Mercer Island, WA 98040; (206) 947-5591
|> The probability of one or more spelling errors in this missive approaches

Nice, but it doesn't account for borrowing across hours, minutes, and 
seconds.  Before the echo's, insert:

if [ $t_sec -lt 0 ]
then
   t_sec=`echo $t_sec+60|bc`
   t_min=`echo $t_min-1|bc`
fi
if [ $t_min -lt 0 ]
then
   t_min=`echo $t_min+60|bc`
   t_hr=`echo $t_hr-1|bc`
fi

That'll fix it up.
-- 
/============================================================================\
|                                            |                               |
|                    ___                     |                               |
|       ___....-----'---`-----....___        |   I haven't lost my mind --   |
| =========================================  |                               |
|        ___`---..._______...---'___         |    it's backed up on          |
|       (___)      _|_|_|_      (___)        |           tape somewhere.     |
|         \\____.-'_.---._`-.____//          |                               |
|           ~~~~`.__`---'__.'~~~~            |   lpeters@marge.hq.af.mil     |
|                   `~~~'                    |                               |
|                                            |     Les Peters                |
\============================================================================/