For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > Re: generation of sequences









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: generation of sequences
William James

2005-11-08, 7:55 am

Sebastian Luque wrote:
> "William James" <w_a_x_man@yahoo.com> wrote:
>
> [...]
>
>
>
>

This "+ 0" isn't necessary.
[color=darkred]

This "+ 0" is necessary because
substr($0,index($0,"*")+1)
is something like
5. Date 27/04/05 start 10:29:00
We only want the 5 at the beginning.
[color=darkred]
>
> May ask why adding zero is necessary when defining these variables?
>
> I've found out that *some* stages are missing the starting date and time
> in several files, so these stage definition lines look like:
>
> Stage 2. Series = 3,4,4,3*10.
>
> In these cases, the starting date and time is given by the next date and
> time in the previously defined series. So if the previous stage had an
> interval of 20 seconds and ended on 25/11/2005,09:00:00,


Wait a minute. Earlier you said:

<< To complicate things a bit, only "Stage 2" blocks have the
right number of lines for the defined time series;
other blocks don't... >>

So if a stage lacks the starting date and time, the previous stage
**must** have been a Stage 2 block; otherwise, the correct number
of lines for that block would be undefined.

Similarly, the last block in the file **must** be a Stage 2.

> then this stage
> would start on 25/11/2005,09:00:20, with its next value being 10 seconds
> later. I think I've allowed for this with the following addition to the
> first pattern/action in your code:
>
>
> --8<---------------cut here---------------start------------->8---
> /^Stage [0-9]/ {
>
>
> oldstage = stage
> oldincrement = increment
> oldstart = start
>
> stage = $2 + 0
> increment = substr($0,index($0,"*")+1) + 0
> if($0 !~ /Date/) next;
> start = get_start_time( $0 )
>
> # If there was a previous stage, and it wasn't
> # a stage 2, we need to generate its lines now.
> if ( oldstage && oldstage != 2 )
> { for (t=oldstart; t<start; t += oldincrement)
> print format_time(t) ",,,,"
> }
>
>
> next
> }
> --8<---------------cut here---------------end--------------->8---
>
> Am I right?


I think so.

Here's my revised version:
------------------------------------------------------------------
/^Stage [0-9]/ {


oldstage = stage
oldincrement = increment
oldstart = start

stage = $2
increment = substr($0,index($0,"*")+1) + 0

if ( !/Date/ )
{ if ( 2 != oldstage )
{
bad_stage( oldstage )
_ERROR_ = 1
exit 1
}
# No starting time given; keep incrementing
# the time we've been using.
next
}

start = get_start_time( $0 )

# If there was a previous stage, and it wasn't
# a stage 2, we need to generate its lines now.
if ( oldstage && oldstage != 2 )
{ for (t=oldstart; t<start; t += oldincrement)
print format_time(t) ",,,,"
}


next
}

2 == stage {
print format_time( start) "," $0
start += increment
}

END {
if ( ! _ERROR_ )
if ( 2 != stage )
bad_stage( stage )
}


function bad_stage( stage )
{ print "\aNonsensical file!" >"/dev/stderr"
printf "Unable to generate lines for stage %d at line %d\n",
stage, FNR >"/dev/stderr"
}


# Input: 26/04/05 start 08:30:00
# Output: 2005 04 26 08 30 00
function fix_date_string( s, a )
{ split( s, a, /[ :\/]/ )
return sprintf( "%s %s %s %s %s %s",
2000+a[3], a[2], a[1], a[5], a[6], a[7] )
}

function get_start_time( s )
{
match( s, /Date / )
return mktime( fix_date_string( substr(s, RSTART+RLENGTH) ))
}

function format_time( timestamp )
{ return strftime( "%d/%m/%Y, %H:%M:%S", timestamp )
}

------------------------------------------------------------------

>
>
> Thanks,
>
> --
> Sebastian P. Luque


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com