1833461 Members
3170 Online
110052 Solutions
New Discussion

Script/AWK help

 
SOLVED
Go to solution
Hoefnix
Honored Contributor

Script/AWK help

Hi,

I am trying to work out a script but not getting the result I want. Maybe you guys can help:

I have a list that contains:
Start month, Start day, Start Hour, Stop Month, Stop day, Stop Hour.

Like next example:
3 29 6 3 29 6
3 29 15 3 29 17
3 29 17 3 29 17
3 29 17 3 29 17
3 29 17 3 29 17
3 29 17 3 29 17
3 29 17 3 29 18
3 29 17 3 31 18
3 29 18 3 29 18
3 29 18 3 29 18
4 1 23 4 1 23
4 1 23 4 1 23
4 1 23 4 1 23
4 1 23 4 1 23
4 1 23 4 2 0
4 2 0 4 2 0
4 2 0 4 2 0

Al these timing represent the start and stop of an application.
I like to figure out how many concurent users I have on an hourly basis on each day i have. So if a user runs the application for 2 day's it will be counted for 1 user per hour during run day's:
The result must look like:

"Month" "day" "hour" "count of concurent users"
3 29 6 2
3 29 7 4
3 29 8 1
...
...
4 1 0 4
etc.


Hope you can help out.
Regards,
Peter
6 REPLIES 6
Franky_1
Respected Contributor

Re: Script/AWK help

Hi Peter,

could you be more precise about getting your desired output
How do you get to your "3 29 8 1" output for example ?

Regards

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: Script/AWK help

Peter,

Your input data's are dealing with starting / stopping time of application. No user details there.

Can your elaborate on your result more.

HTH.
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor
Solution

Re: Script/AWK help

Hey Peter, With all due respect, it looks to me you gave a lousy input example!
I think you are looking for something like this perl script:

use Time::Local;
while (<>) {
($bm, $bd, $bh, $em, $ed, $eh) = split;
$time = timelocal(0,0,$bh,$bd,$bm-1,2004);
$end = timelocal(0,0,$eh,$ed,$em-1,2004);
while ($time < $end) {
($sec,$min,$hour,$mday,$mon,$year) = localtime $time;
$key = sprintf ("%02d %02d %02d", $mon+1, $mday, $hour);
$usage{$key}++;
$time +=3600;
}
}

foreach $time (sort keys %usage) {
print "$time $usage{$time}\n";
}

For each data line it takes each begin and end time and converts it to seconds.
Then it adds one to the array entry for the start second for each hour in the range.
When all lines are doen it reports the accumulate users for each start hour it ever touched.

With this input:

3 30 6 3 30 16
3 30 14 3 30 17
3 30 18 3 30 19
3 30 13 4 01 18
4 1 10 4 1 23
4 1 20 4 1 21

The output becomes:

03 30 06 1
03 30 07 1
03 30 08 1
03 30 09 1
03 30 10 1
03 30 11 1
03 30 12 1
03 30 13 2
03 30 14 3
03 30 15 3
03 30 16 2
03 30 17 1
03 30 18 2
03 30 19 1
03 30 20 1
:
04 01 09 1
04 01 10 2
04 01 11 2
04 01 12 2
04 01 13 2
04 01 14 2
04 01 15 2
04 01 16 2
04 01 17 2
04 01 18 1
04 01 19 1
04 01 20 2
04 01 21 1
04 01 22 1

Met vriendelijke groetjes,
En veel geluk er mee!
:-).

Hein.
Hoefnix
Honored Contributor

Re: Script/AWK help

Hein,

It looks to me that you got the correct output from my lousy input. The input file I've got is very large so I just cut and pasted some small part. I will test your solution tomorrow because I am in a meeting right now. Thanks anyway.

Franky,
The output have to name the number of runs of an application within that hour of a day.

Muthukumar,
I have user data in this file, but it does not interest me at the moment because the same user can run multiple session of this application. The stats show them per session as one row.

Thanks all and I report back tomorrow,
Peter
Hein van den Heuvel
Honored Contributor

Re: Script/AWK help

>> It looks to me that you got the correct output from my lousy input.

:-)

My biggest problem was that the suggested output did not line up with the suggested input. That, plus the fact that the sample input consisted mostly of 'null' records (usage less than 1 full hour?).

btw....

Just in case anyone ever needs a thing smilar to this being year-end resilient...
Here is a tweak that will make that happen.
(Also, it should really use the actual year ,not my hardcoded 2004, to deal with leap years properly :-)


-------------------- perl script ----------
use Time::Local;
while (<>) {
($bm, $bd, $bh, $em, $ed, $eh) = split;
$time = timelocal(0,0,$bh,$bd,$bm-1,2004);
$end = timelocal(0,0,$eh,$ed,$em-1,($em<$bm)? 2005: 2004);
while ($time < $end) {
($sec,$min,$hour,$mday,$mon,$year) = localtime $time;
$key = sprintf ("%1d %02d %02d %02d", $year-104,$mon+1, $mday, $hour);
$usage{$key}++;
$time +=3600;
}
}
foreach $time (sort keys %usage) {
print "$time $usage{$time}\n";
}
---------------------- sample input --------
3 31 20 3 31 23
3 31 18 3 31 22
3 31 21 4 01 6
12 31 21 01 01 01
12 31 22 12 31 23
------output for that sample input --------
0 03 31 18 1
0 03 31 19 1
0 03 31 20 2
0 03 31 21 3
0 03 31 22 2
0 03 31 23 1
0 04 01 00 1
0 04 01 01 1
0 04 01 02 1
0 04 01 03 1
0 04 01 04 1
0 04 01 05 1
0 12 31 21 1
0 12 31 22 2
0 12 31 23 1
1 01 01 00 1

Cheers,
Hein.
Hoefnix
Honored Contributor

Re: Script/AWK help

Hein,

Your scripts work fine. There is one remark. You already mentioned it in the second post, but the times that an application runs within the same hour needs also be counted for that hour.
With this script these stats are not comming back in the output.

Thanks anyway because I would not have been succesful writing this myself. I will try to fix the counts of less then 1 hour runs.

Regards(vriendelijke groet),
Peter