home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!think.com!spool.mu.edu!wupost!csus.edu!netcom.com!dman
- From: dman@netcom.com (Dallman Ross)
- Subject: Re: Performing simple math on a numeric file from csh
- Message-ID: <1992Dec25.190033.27265@netcom.com>
- Organization: Netcom - Online Communication Services (408 241-9760 guest)
- X-Newsreader: TIN [version 1.1 PL8]
- References: <1992Dec16.080343.24756@netcom.com>
- Date: Fri, 25 Dec 1992 19:00:33 GMT
- Lines: 77
-
- 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
- : >
-
- Here is the wonderful awk answer sent to me by Lou Kates, aka
- louk@research.teleride.on.ca:
-
- # Get time difference between dates in last login and logout record.
- # It is assumed that the last logout record occurred this year but the
- # last login record could have occurred either this year or the previous
- # year.
-
- # build an array whose subscripts are the words in str and whose values
- # are 1, 2, 3, ...
- function asplit(str, arr, temp, n) {
- n = split(str, temp);
- for (i=1; i <= n; i++)
- arr[temp[i]] = i;
- return n;
- }
-
- # number of seconds since Jan 1, 1901. Mon is "Jan", "Feb", etc. Other
- # args are numbers.
- function secs(y, mon, d, h, min, s, months, days, i, n) {
- asplit("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", months);
- m = months[mon];
- split("31 28 31 30 31 30 31 30 31 31 30 31 30 31", days);
- n = (y-1901) * 365 + int((y-1901)/4);
- if (y%4 == 0) days[2]++;
- for (i=1; i<m; i++)
- n += days[i];
- return (n+d)*24*60*60 + 60*60*h + 60*min + s;
- }
-
- # get today's year. If not using UNIX replace next line with BEGIN { yr=1992 }
- # where 1992 is replaced with this year's year.
- BEGIN { "date" | getline datstr; split(datstr, dat); yr = dat[6]; }
-
- # calculate seconds since Jan 1, 1901 of the login
- $1 == "login:" { split($4, lin, ":");
- monin = $2; dayin = $3;
- secin = secs(yr, monin, dayin, lin[1], lin[2], lin[3]);
- }
-
- # calculate seconds since Jan 1, 1901 of the logout. If it is less than
- # login seconds then login must have occurred in previous year so recalc it
- $1 == "logout:" { split($4, lout, ":");
- secout = secs(yr, $2, $3, lout[1], lout[2], lout[3]);
- # if secout < secin then login must be in previous year
- if (secin > secout)
- secin = secs(yr-1, monin, dayin, lin[1], lin[2], lin[3]);
- }
-
- END { sec = secout - secin;
- printf "%02d:%02d:%02d\n", int(sec/3600), int(sec/60)%60, sec%60;
- }
-
- --
- __D_a l_l m a_n _ R o_s s _ |dman@netcom.com /or/ |"You sound like a man|
- l \\ l\\ /l /\\ l\\ l |dross@well.sf.ca.us |with a rubber nose." |
- l >)l \\ /ll / \\ l \\ l |vox/fax: 1.510.645.1883| -- One-Lung Bill |
- l // l \X ll/--- \\l \\ l |350 Perkins St., #108 | Remmer (deceased)
- _l//______________________\\l_|Oakland,_CA__94610-3422|_____________________|_
-