Block processing using awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Block processing using awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 06:46 AM..
Sponsored Links
    #2  
Old 01-16-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 December 2014, 6:56 AM EST
Location: Aachen, Germany
Posts: 4,886
Thanks: 80
Thanked 1,224 Times in 1,150 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 01-16-2013
Jotne's Avatar
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 03: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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 10:17 AM
processing the output of AWK rakeshkumar Shell Programming and Scripting 4 10-04-2011 11:17 PM
awk: How to search for a block within a block ? DerekAsirvadem Shell Programming and Scripting 5 01-11-2011 09:18 AM
Processing with awk zainravi Shell Programming and Scripting 2 02-24-2009 11:18 AM
awk processing pxy2d1 Shell Programming and Scripting 7 08-21-2008 02:09 PM



All times are GMT -4. The time now is 07:25 PM.