开发者

Find max number of concurrent events

I'd like to print the max number of concurrent events given the start time and 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, I would

  • Sort by start time (column 1)
  • Use awk/sed to iterate over all values in column 2 (i.e end time) to find the count of end times preceeding this event which are greater than the current value (i.e find all currently running events). To elaborate, assuming line 3 was being processed by awk ... Its end time is 10:33. The end times of the preceding 2 events are 11:00 and 11:00. Since both these values are greater than 10:33 (i.e. they are still running at 10:33),开发者_开发技巧 the third column (i.e. number of concurrent jobs) would contain 2 for this line

The expected output of the awk script to find concurrent events for this input would be

0
1
2
2
2
4
0 
  • Find the max value of this third column.

My awk is rudimentary at best and I am having difficulty implementing step 2. I'd like this to be a pure script without resorting to a heavy weight language like java. Hence any help from awk gurus would be highly appreciated. Any non-awk linux one liners are also most welcome.


BEGIN  {FS="\,"; i=0}

{ superpos=0;
    for (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; 
        for (j=1; j<=i; j++ ){ 
          if ( a[j,3]>max)
            max= a[j,3];
     }
     print "max = ",max;
 }

Running at ideone

HTH!

Output:

0
0
2
2
2
4
0
max =  4

Edit

Or more awkish, if you prefer:

BEGIN  {FS="\,"; max=0 }
{ 
    b=0; 
    for (var in a){
      if($2 < a[var]) b++;
    }  
      a[NR]=$2;
      print b;
      if (b > max) max = b;
}
END { print "max = ", max }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜