Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Block processing using awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-16-2013
sai_2507 sai_2507 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 19 December 2013, 10:47 PM EST
Posts: 73
Thanks: 18
Thanked 1 Time in 1 Post
Block processing using awk

Hi,

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

For example,

Sample File:


Code:
 
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


Code:
 
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


Last edited by Scrutinizer; 01-16-2013 at 05:46 AM..
Sponsored Links
    #2  
Old Unix and Linux 01-16-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 3 July 2015, 9:53 AM EDT
Location: Aachen, Germany
Posts: 6,243
Thanks: 109
Thanked 1,700 Times in 1,592 Posts
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:
Code:
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

The Following User Says Thank You to RudiC For This Useful Post:
sai_2507 (01-16-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-16-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
With time difference, will work pass midnight.

Code:
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:

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


Last edited by Jotne; 01-16-2013 at 02:14 PM.. Reason: Cleaned code some
The Following User Says Thank You to Jotne For This Useful Post:
sai_2507 (01-16-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
processing with awk esolvepolito Shell Programming and Scripting 9 08-23-2012 09:17 AM
processing the output of AWK rakeshkumar Shell Programming and Scripting 4 10-04-2011 10:17 PM
awk: How to search for a block within a block ? DerekAsirvadem Shell Programming and Scripting 5 01-11-2011 08:18 AM
Processing with awk zainravi Shell Programming and Scripting 2 02-24-2009 10:18 AM
awk processing pxy2d1 Shell Programming and Scripting 7 08-21-2008 01:09 PM



All times are GMT -4. The time now is 10:51 PM.