bash - Find max number of concurrent events -
i'd print max number of concurrent events given start time , end time of each event in "hhmm" format (example input below)
$ cat input.txt 1030,1100 1032,1100 1032,1033 1033,1050 1034,1054 1039,1043 1040,1300
for this, would
- sort start time (column 1)
- use awk/sed iterate on values in column 2 (i.e end time) find count of end times preceeding event greater current value (i.e find running events). elaborate, assuming line 3 being processed awk ... end time 10:33. end times of preceding 2 events 11:00 , 11:00. since both these values greater 10:33 (i.e. still running @ 10:33), third column (i.e. number of concurrent jobs) contain 2 line
the expected output of awk script find concurrent events input be
0 1 2 2 2 4 0
- find max value of third column.
my awk rudimentary @ best , having difficulty implementing step 2. i'd pure script without resorting heavy weight language java. hence awk gurus highly appreciated. non-awk linux 1 liners welcome.
begin {fs="\,"; i=0} { superpos=0; (j=1; j<=i; j++ ){ if($2 < a[j,2]) ++superpos } a[++i,1]=$1; a[i,2]=$2; print superpos; a[i,3]=superpos; } end{ max=0; (j=1; j<=i; j++ ){ if ( a[j,3]>max) max= a[j,3]; } print "max = ",max; }
running @ ideone
hth!
output:
0 0 2 2 2 4 0 max = 4
edit
or more awkish, if prefer:
begin {fs="\,"; max=0 } { b=0; (var in a){ if($2 < a[var]) b++; } a[nr]=$2; print b; if (b > max) max = b; } end { print "max = ", max }
Comments
Post a Comment