|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 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 |
|
|