Help calculating time with awk 
Author Message
 Help calculating time with awk

I was wondering if someone out ther could help me write a script to
add time from a report file.

here is my report file.

No Communication to VCO at 09:44:37
No Communication to VCO at 09:45:57
No Communication to VCO at 09:53:37
VCO communications reastablished at 09:53:35
No Communication to VCO at 10:44:00
No Communication to VCO at 10:45:57
No Communication to VCO at 10:53:37
VCO communications reastablished at 10:54:50

I wrote this but it does not allow for more than one outage in a day .

excuse the formatting , i am learning that too.

BEGIN {p = 0}

{ if ( $0 ~ /No/ && $0 ~ /VCO/) then ( itime = $6)}
{print itime}
{p = 1}

{ while ( p = 0 ) next; }

{ if ( $0 ~ /VCO/ && $0 ~ /reastablished/) then (etime = $5)}

{print etime }
{p = 1}

function pdd( pd_s, pd_e )
{
if( split( pd_s, pdin, ":" )  !=3 ) return;
if( split( pd_e, pdout, ":" )  !=3 ) return;

hh = pdout [1] - pdin[1];
mm = pdout [2] - pdin[2];
ss = pdout [3] - pdin[3];
pddiff = 60 * ( hh * 60 + mm ) + ss;

printf( "%s> %d sec\n", pd_s, pddiff );

Quote:
}

# the time function call below
pdd( itime,etime)

END {print"starttime", itime
print "endtime",etime
print "the downtime is ",pddiff",secs"}

I basically need a way to store the first time "No Communication to
VCO at " time .
then the first " Communication  reastablished at" time and pass this
to the pdd (time subtract function)

but on top of this i need to keep the output from function for later
to be added if there are any more outages.

I was told it was a simple thing to do but it looks very complicated
to me.

Any help would be very much appreciated.

Nev.



Thu, 30 Jan 2003 03:00:00 GMT  
 Help calculating time with awk

% here is my report file.
%
% No Communication to VCO at 09:44:37
% No Communication to VCO at 09:45:57
% No Communication to VCO at 09:53:37
% VCO communications reastablished at 09:53:35
[...]

% BEGIN {p = 0}
%
% { if ( $0 ~ /No/ && $0 ~ /VCO/) then ( itime = $6)}

A few things here. First, awk scripts have the form
 pattern action
 pattern2 action2

Except for the special patterns BEGIN and END, each pattern is evaluated
once per input line, and the action is executed if the pattern evalutes
to a non-zero value. If there is no pattern, the action is always
executed. This action is equivalent to
 $0 ~ /No/ && $0 ~ /VCO/ { then ( itime = $6) }

This version is more in the style of the language, and possibly more
efficient. As a short-cut, you can write just /No/ for $0 ~ /No/.

Second, the syntax of an awk if statement is
 if (pattern) action
then is not a keyword. In this case, it turns awk evalues the variable
then and concatenates it to the result of (itime = $6), setting itime to
the value of $6 as a side-effect.

Third, it's probably more efficient to test a single regular expression
against $0, rather than two expressions, so this line could be
 /No.*VCO/ { itime = $6 }

% {print itime}
% {p = 1}

These lines will be evalutated for every input line. itime will always be
printed, and p will be set to 1 on the first input line and never change
back to 0.

% { while ( p = 0 ) next; }

You want your test to be p == 0. This version sets p to 0 and then evaluates
it (the result is 0, which means the next will never be executed).

Once the test is fixed, next is _still_ never executed because p will always
be 1 by the time you reach this line. Finally, the while might as well be
if, since next breaks out of any loops, so
  while (whatever) next
can never be executed more than once.

If your goal is to print the interruption time, you could do this:
 BEGIN { p = 0 }
 !p && /No.*VCO/ { print itime = $6; p = 1; next }

% { if ( $0 ~ /VCO/ && $0 ~ /reastablished/) then (etime = $5)}
%
% {print etime }
% {p = 1}

The same comments apply. Also, you want to re-set p to 0 at this point.
This line should do it.

 # note reastablished should be spelled reestablished
 /VCO.*reastablished/ { print etime = $5; p = 0; ppd(itime,etime) }

% hh = pdout [1] - pdin[1];

Suggestion: you should handle negatives here (eg, failed at 23.23 and
re-established at 0.14).

% but on top of this i need to keep the output from function for later
% to be added if there are any more outages.

You should return it, then. Here's a completed script:

 BEGIN { p = 0; downtime = 0 }

 # save the interruption time the first time it's reported
 !p && /No.*VCO/ { print itime = $6; p = 1 }

 # calculate the down-time when the link is re-established,
 # and reset p so we know there hasn't been an interruption reported
 /VCO.*reastablished/ { print etime = $5; p = 0;
       downtime += pdd(itime,etime)
 }

 # calcuate the difference in time and print the value
 function pdd( pd_s, pd_e,  hh, mm, ss, pddiff )
 {
    if( split( pd_s, pdin, ":" )  !=3 ) return 0;
    if( split( pd_e, pdout, ":" )  !=3 ) return 0;

    hh = pdout [1] - pdin[1];
    if (hh < 0) hh += 24     # assume down-time less than 1 day

    mm = pdout [2] - pdin[2];
    ss = pdout [3] - pdin[3];

    pddiff = 60 * ( hh * 60 + mm ) + ss;

    print "the downtime is", pddiff, "secs"
    return pddiff
 }

 END { print "total downtime is", downtime, "secs" }
--

Patrick TJ McPhee
East York  Canada



Thu, 30 Jan 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help Calculating Time

2. Need help on calculating time

3. awk Help format time

4. Calculating Time Elapsed

5. Calculating Time

6. calculating times over midnight

7. Calculating elapsed time

8. to calculate execution time in Fortran 77

9. Calculating a program's run time

10. How to calculate CPU time

11. calculate time span

12. Arrays in awk/awk help please!

 

 
Powered by phpBB® Forum Software