home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / question / 14971 < prev    next >
Encoding:
Text File  |  1992-12-25  |  3.2 KB  |  89 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!think.com!spool.mu.edu!wupost!csus.edu!netcom.com!dman
  3. From: dman@netcom.com (Dallman Ross)
  4. Subject: Re: Performing simple math on a numeric file from csh
  5. Message-ID: <1992Dec25.190033.27265@netcom.com>
  6. Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
  7. X-Newsreader: TIN [version 1.1 PL8]
  8. References: <1992Dec16.080343.24756@netcom.com>
  9. Date: Fri, 25 Dec 1992 19:00:33 GMT
  10. Lines: 77
  11.  
  12. I, Dallman Ross (dman@netcom.com), wrote:
  13. : I am trying to understand awk, but the man pages are driving me nuts. 
  14. : Anyway, I have a couple of simple files with numerics in them that I
  15. : want to do some math with.  This doesn't have to be via awk, but I've
  16. : been told that's the ticket.  Anyway, I use csh.
  17. :  
  18. : Here's what one of the files looks like:
  19. :  
  20. : > login:  Dec 15 21:40:01
  21. : > logout: Dec 15 21:40:18
  22. : > 
  23. : > login:  Dec 15 21:41:11
  24. : > logout: Dec 15 21:41:48
  25. : > 
  26. : > login:  Dec 15 22:20:08
  27. : > logout: Dec 15 22:31:44
  28. : > 
  29.  
  30. Here is the wonderful awk answer sent to me by Lou Kates, aka
  31. louk@research.teleride.on.ca:
  32.  
  33. # Get time difference between dates in last login and logout record.
  34. # It is assumed that the last logout record occurred this year but the
  35. # last login record could have occurred either this year or the previous
  36. # year.
  37.  
  38. # build an array whose subscripts are the words in str and whose values
  39. # are 1, 2, 3, ...
  40. function asplit(str, arr,    temp, n) {
  41.     n = split(str, temp);
  42.     for (i=1; i <= n; i++)
  43.     arr[temp[i]] = i;
  44.     return n;
  45. }
  46.  
  47. # number of seconds since Jan 1, 1901.  Mon is "Jan", "Feb", etc.  Other
  48. # args are numbers.
  49. function secs(y, mon, d, h, min, s,    months, days, i, n) {
  50.     asplit("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", months);
  51.     m = months[mon];
  52.     split("31 28 31 30 31 30 31 30 31 31 30 31 30 31", days);
  53.     n = (y-1901) * 365 + int((y-1901)/4);
  54.     if (y%4 == 0) days[2]++;
  55.     for (i=1; i<m; i++)
  56.     n += days[i];
  57.     return (n+d)*24*60*60 + 60*60*h + 60*min + s;
  58. }
  59.  
  60. # get today's year.  If not using UNIX replace next line with BEGIN { yr=1992 }
  61. # where 1992 is replaced with this year's year.
  62. BEGIN { "date" | getline datstr; split(datstr, dat); yr = dat[6]; }
  63.  
  64. # calculate seconds since Jan 1, 1901 of the login
  65. $1 == "login:" { split($4, lin, ":"); 
  66.          monin = $2; dayin = $3;
  67.          secin = secs(yr, monin, dayin, lin[1], lin[2], lin[3]);
  68.            }
  69.  
  70. # calculate seconds since Jan 1, 1901 of the logout.  If it is less than
  71. # login seconds then login must have occurred in previous year so recalc it
  72. $1 == "logout:" { split($4, lout, ":"); 
  73.           secout = secs(yr, $2, $3, lout[1], lout[2], lout[3]);
  74.           # if secout < secin then login must be in previous year
  75.           if (secin > secout)
  76.                    secin = secs(yr-1, monin, dayin, lin[1], lin[2], lin[3]);
  77.         }
  78.                   
  79. END { sec = secout - secin;
  80.       printf "%02d:%02d:%02d\n", int(sec/3600), int(sec/60)%60, sec%60;
  81. }
  82.  
  83. --
  84.  __D_a l_l m a_n  _ R o_s s _ |dman@netcom.com  /or/  |"You sound like a man|
  85.  l  \\ l\\   /l  /\\  l\\   l |dross@well.sf.ca.us    |with a rubber nose." |
  86.  l   >)l \\ /ll /  \\ l \\  l |vox/fax: 1.510.645.1883| -- One-Lung Bill    |
  87.  l  // l  \X ll/--- \\l  \\ l |350 Perkins St., #108  |     Remmer (deceased)
  88. _l//______________________\\l_|Oakland,_CA__94610-3422|_____________________|_
  89.