Home > Archive > AWK > November 2005 > more 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 |
more generation of sequences
|
|
| Sebastian Luque 2005-11-15, 3:55 am |
| Hello,
I need to generate time series in a new set of files that look like this:
--8<---------------cut here---------------start------------->8---
25/08/2005,11:00:10,field3,field4
25/08/2005,11:01:10,field3
25/08/2005,11:02:10,field3,field4,field5
25/08/2005,11:05:10,field3,field4
25/08/2005,11:06:10,field3
25/08/ 2005,11:09:10,field3,field4,field5,field
6
,
--8<---------------cut here---------------end--------------->8---
and my goal is to generate a *continuous* time series using only the data
from lines that have exactly 3 fields, ignoring the data in subsequent
fields. So the output for the above lines would be:
--8<---------------cut here---------------start------------->8---
25/08/2005,11:00:10,field3
25/08/2005,11:01:10,field3
25/08/2005,11:02:10,field3
25/08/2005,11:03:10,
25/08/2005,11:04:10,
25/08/2005,11:05:10,field3
25/08/2005,11:06:10,field3
25/08/2005,11:07:10,
25/08/2005,11:08:10,
25/08/2005,11:09:10,field3
25/08/2005,11:10:10,
--8<---------------cut here---------------end--------------->8---
I thought I could hack William's code a bit to do that (the increment is
known a priori in this case):
--8<---------------cut here---------------start------------->8---
BEGIN {
FS = OFS = ","
increment = 60
}
{
oldbeg = beg
if (NF != 3 || $1 == "") next
beg = beg_time($1, $2)
if (beg > oldbeg + increment) {
for (i = oldbeg + increment; i < beg; i += increment)
print format_time(i) ","
} else {
print
}
}
# Input: 26/04/2005 08:30:00
# Output: 2005 04 26 08 30 30
function fix_date_string(s) {
split(s, a, /[ :\/]/)
return sprintf("%s %s %s %s %s %s",
a[3], a[2], a[1], a[5], a[6], a[7])
}
function beg_time(d, t) {
timestr = d " " t
return mktime(fix_date_string(timestr))
}
function format_time(timestamp) {
return strftime("%d/%m/%Y,%H:%M:%S", timestamp);
}
--8<---------------cut here---------------end--------------->8---
but this is simply taking the rows with 3 fields and ignoring the
generation of time series. Thanks again for any help.
Cheers,
--
Sebastian P. Luque
| |
| Sebastian Luque 2005-11-15, 6:55 pm |
| Sebastian Luque <spluque@gmail.com> wrote:
[...]
> # Input: 26/04/2005 08:30:00
> # Output: 2005 04 26 08 30 30
> function fix_date_string(s) {
> split(s, a, /[ :\/]/)
> return sprintf("%s %s %s %s %s %s",
> a[3], a[2], a[1], a[5], a[6], a[7])
> }
Well, I spotted an error in the array references above, which should be 4,
5, and 6 (instead of 5, 6, and 7). But still struggling.
--
Sebastian P. Luque
| |
| Steffen Schuler 2005-11-16, 3:55 am |
| Sebastian Luque wrote:
> I need to generate time series in a new set of files that look like this:
>
>
> --8<---------------cut here---------------start------------->8---
> 25/08/2005,11:00:10,field3,field4
> 25/08/2005,11:01:10,field3
> 25/08/2005,11:02:10,field3,field4,field5
> 25/08/2005,11:05:10,field3,field4
> 25/08/2005,11:06:10,field3
>
> 25/08/ 2005,11:09:10,field3,field4,field5,field
6
> ,
> --8<---------------cut here---------------end--------------->8---
>
> and my goal is to generate a *continuous* time series using only the data
> from lines that have exactly 3 fields, ignoring the data in subsequent
> fields. So the output for the above lines would be:
>
>
> --8<---------------cut here---------------start------------->8---
> 25/08/2005,11:00:10,field3
> 25/08/2005,11:01:10,field3
> 25/08/2005,11:02:10,field3
> 25/08/2005,11:03:10,
> 25/08/2005,11:04:10,
> 25/08/2005,11:05:10,field3
> 25/08/2005,11:06:10,field3
> 25/08/2005,11:07:10,
> 25/08/2005,11:08:10,
> 25/08/2005,11:09:10,field3
> 25/08/2005,11:10:10,
> --8<---------------cut here---------------end--------------->8---
>
>
> I thought I could hack William's code a bit to do that (the increment is
> known a priori in this case):
>
> --8<---------------cut here---------------start------------->8---
> BEGIN {
> FS = OFS = ","
> increment = 60
> }
>
> {
> oldbeg = beg
> if (NF != 3 || $1 == "") next
> beg = beg_time($1, $2)
> if (beg > oldbeg + increment) {
> for (i = oldbeg + increment; i < beg; i += increment)
> print format_time(i) ","
> } else {
> print
> }
> }
>
> # Input: 26/04/2005 08:30:00
> # Output: 2005 04 26 08 30 30
> function fix_date_string(s) {
> split(s, a, /[ :\/]/)
> return sprintf("%s %s %s %s %s %s",
> a[3], a[2], a[1], a[5], a[6], a[7])
> }
>
> function beg_time(d, t) {
> timestr = d " " t
> return mktime(fix_date_string(timestr))
> }
>
> function format_time(timestamp) {
> return strftime("%d/%m/%Y,%H:%M:%S", timestamp);
> }
> --8<---------------cut here---------------end--------------->8---
>
>
> but this is simply taking the rows with 3 fields and ignoring the
> generation of time series. Thanks again for any help.
Hi Sebastian,
try that:
#!/usr/bin/gawk -f
BEGIN {
FS = OFS = ","
increment = 60
}
{
oldbeg = beg
if (NF < 3 || $1 == "") next
beg = beg_time($1, $2)
if (oldbeg != 0 && beg > oldbeg + increment) {
for (i = oldbeg + increment; i < beg; i += increment)
print format_time(i) ","
}
print $1, $2, $3
}
# Input: 26/04/2005 08:30:00
# Output: 2005 04 26 08 30 30
function fix_date_string(s, a) {
split(s, a, /[ :\/]/)
return sprintf("%s %s %s %s %s %s",
a[3], a[2], a[1], a[4], a[5], a[6])
}
function beg_time(d, t) {
timestr = d " " t
return mktime(fix_date_string(timestr))
}
function format_time(timestamp) {
return strftime("%d/%m/%Y,%H:%M:%S", timestamp);
}
Regards,
Steffen
| |
| Sebastian Luque 2005-11-16, 6:55 pm |
| Steffen Schuler <schuler.steffenDELETETHIS@gmx.de> wrote:
[...]
> #!/usr/bin/gawk -f
> BEGIN {
> FS = OFS = ","
> increment = 60
> }
> {
> oldbeg = beg
> if (NF < 3 || $1 == "") next
> beg = beg_time($1, $2)
> if (oldbeg != 0 && beg > oldbeg + increment) {
> for (i = oldbeg + increment; i < beg; i += increment)
> print format_time(i) ","
> }
> print $1, $2, $3
> }
> # Input: 26/04/2005 08:30:00
> # Output: 2005 04 26 08 30 30
> function fix_date_string(s, a) {
> split(s, a, /[ :\/]/)
> return sprintf("%s %s %s %s %s %s",
> a[3], a[2], a[1], a[4], a[5], a[6])
> }
> function beg_time(d, t) {
> timestr = d " " t
> return mktime(fix_date_string(timestr))
> }
> function format_time(timestamp) {
> return strftime("%d/%m/%Y,%H:%M:%S", timestamp);
> }
Thank you Steffen, that works.
Cheers,
--
Sebastian P. Luque
|
|
|
|
|