Operating System - HP-UX
1831339 Members
3386 Online
110024 Solutions
New Discussion

need help on ksh scripting

 
YLTan
Frequent Advisor

need help on ksh scripting


Does anyone have any good suggestion on calculating the duration for the data below using ksh scripts? I am think of using year, month, day, hour and minute for comparison to find the duration. Does anyone have any good suggestion such as using awk, sed, sort, etc..

08/22/02|00:00| 300| 100| <---- 1st duration

09/22/02|00:05| 299| 100| <---- 2nd duration
09/22/02|00:10| 299| 100|

11/12/02|00:15| 300| 100| <---- 3rd duration

12/30/02|23:50| 299| 100| <---- 4th duration

12/31/02|23:50| 299| 100| <---- 5th duration
12/31/02|23:55| 299| 100|
01/01/03|00:00| 299| 100|
01/01/03|00:05| 299| 100|

01/01/03|00:35| 299| 100| <---- 6th duration

02/19/03|00:40| 300| 100| <---- 7th duration

03/03/03|00:45| 299| 100| <---- 8th duration
03/03/03|00:50| 299| 100|

03/11/03|00:55| 299| 100| <---- 9th duration
tyl
5 REPLIES 5
Graham Cameron_1
Honored Contributor

Re: need help on ksh scripting

Can't figure out what is in cols 3 & 4?
Please explain the data format.
After that it should be easy with awk -F"|"
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
YLTan
Frequent Advisor

Re: need help on ksh scripting


1st col is date in mm/dd/yy
2nd col is 24hrs time in hh:mm
3rd col is data
4th col is also max CPU%

i need to output the report of the duration of the max data e.g.

Start 12/31/02 23:50 and End 01/01/03 00:05 - 15minutes when CPU% is at max

Start 01/01/03 00:05 - 5 minutes when CPU% is max
tyl
Graham Cameron_1
Honored Contributor

Re: need help on ksh scripting

Ok, I think I understand.
Every time col 3 changes you want a report of the time since it last changed.
This is not trivial with awk and I'm no Perl wizard.
I suggest you wait a couple of hours until the US comes online, and Clay will solve your problem with his caljd script.
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Eric Buckner
Regular Advisor

Re: need help on ksh scripting

First think I would do is get my hands on Gnu Date. It has the ability to convert a date to seconds since epoch. For your hours and seconds those are easily converted to seconds.

The syntax to do this w/ the Gnu date command is:
date -d "mm/dd/yy HH:MM:SS" +%s

From your example:

12/31/02|23:55| 299| 100|
01/01/03|00:00| 299| 100|
01/01/03|00:05| 299| 100|
01/01/03|00:35| 299| 100| <---- 6th duration


In your example 12/31/02 23:55 is equal to 1,041,400,500 seconds since epoch. And 01/01/03 00:35 is 1,041,402,900 seconds since epoch. Subtract the prior from the later and you get 2,400 seconds duration that it was at 299. You can then just roll up the 2,400 to days, hours, minutes, seconds by dividing it out.

1 day = 86,400 seconds
1 hour = 3,600 seconds
1 minute = 60 seconds (of course)

So 2,400 seconds is 40 minutes.

Gnu Date is part of sh_utils on the HP Porting and archive center.

http://hpux.cs.utah.edu/hppd/hpux/Gnu/sh_utils-2.0/

Hope that helps!
Eric
Time is not a test of the truth.
curt larson_1
Honored Contributor

Re: need help on ksh scripting

you could use A. Clay Stephenson's caljd.sh script. just do a search on caljd.sh. just be sure to get the latest version intead of an earlier version that was posted quite a while ago. I think he has a site where you can download the latest version.

or

you could use perl and as suggested in the previous post, use the localtime and timelocal functions to convert your dates to seconds from the epoch, do your arthimetic, and format to your desired output