Block processing using awk

Hi,

I want to know how to process block of lines using awk.

For example,

Sample File:

 
20130113 18:07:50 PROCESS START
ABC
XYZ
DEF
20130113 18:07:52 PROCESS END for ID 123

20130113 18:07:52 PROCESS START
ABC
XYZ
DEF
20130113 18:07:53 PROCESS END for ID 124
 
Desired Output:
ID|Start_Time|End_Time|Duration(End_Time-Start_Time)
123|18:07:50|18:07:52|2
124|18:07:52|18:07:53 |1

For the duration you need complex date/time arithmetics to take into account e.g. durations crossing midnight. There's quite some threads on this available, pls. search this forum.
However, try this:

awk 'BEGIN     {print "ID|Start_Time|End_Time|Duration"}
     /START/   {st=$2}
     /END for/ {en=$2; ID=$NF; print ID"|"st"|"en"|"0; st=en=ID=""}
    ' file
ID|Start_Time|End_Time|Duration
123|18:07:50|18:07:52|0
124|18:07:52|18:07:53|0
1 Like

With time difference, will work pass midnight.

awk 'BEGIN {print "ID|Start_Time|End_Time|Duration"}
     /START/   {st=$2}
     /END for/ {en=$2;
     ID=$NF;
     split(st,s,":"); split(en,e,":");
     {if (e[1]<s[1]) {e[1]+=24}};
     diff=(e[1]*3600+e[2]*60+e[3])-(s[1]*3600+s[2]*60+s[3]);
     print ID"|"st"|"en"|"diff;
     st=en=ID=""}'

Result:

123|18:07:50|18:07:52|2
124|18:07:52|18:07:53|1
1 Like