Unix Shell - transfering time format in gawk ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > transfering time format in gawk ?





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 transfering time format in gawk ?
lihao0129@gmail.com

2007-12-08, 7:42 pm

Hi, folks:

I am using gawk to parse a log file which I need to sort the results
by the timestamp. see, the current time format is:

08/Dec/2007:21:08:50

I want to transfer it into

2007 12 08 21 08 50

so that I can use gawk's mktime function.. Currently I had a function:

function get_time(t)
{
split(t,dt,"[/:]")
dtime = sprintf("%d %d %d %d %d %d",
dt[3],mon[dt[2]],dt[1],dt[4],dt[5],dt[6])
return mktime(dtime)
}

and in BEGIN block,

BEGIN {
mon["Jan"] = 1; .....; mon["Dec"] = 12;
}

This seems ok but looked really ugly, can someone recommend me some
better and more efficient ways to handle this?

Thanks a lot;

LH
Ed Morton

2007-12-09, 1:35 am



On 12/8/2007 3:33 PM, lihao0129@gmail.com wrote:
> Hi, folks:
>
> I am using gawk to parse a log file which I need to sort the results
> by the timestamp. see, the current time format is:
>
> 08/Dec/2007:21:08:50
>
> I want to transfer it into
>
> 2007 12 08 21 08 50
>
> so that I can use gawk's mktime function.. Currently I had a function:
>
> function get_time(t)
> {
> split(t,dt,"[/:]")
> dtime = sprintf("%d %d %d %d %d %d",
> dt[3],mon[dt[2]],dt[1],dt[4],dt[5],dt[6])
> return mktime(dtime)
> }
>
> and in BEGIN block,
>
> BEGIN {
> mon["Jan"] = 1; .....; mon["Dec"] = 12;
> }
>
> This seems ok but looked really ugly, can someone recommend me some
> better and more efficient ways to handle this?
>
> Thanks a lot;
>
> LH


No need for the BEGIN block:

function get_time(t, dt) {
split(t,dt,"[/:]")
match("JanFebMarAprMayJunJulAugSepOctNovDec",dt[2])
dt[2] = sprintf("%02d",(RSTART+2)/3)
return( mktime(dt[3]" "dt[2]" "dt[1]" "dt[4]" "dt[5]" "dt[6]) )
}

Regards,

Ed.

Steffen Schuler

2007-12-09, 1:35 am

On Sat, 08 Dec 2007 13:33:48 -0800, lihao0129@gmail.com wrote:

> Hi, folks:
>
> I am using gawk to parse a log file which I need to sort the results by
> the timestamp. see, the current time format is:
>
> 08/Dec/2007:21:08:50
>
> I want to transfer it into
>
> 2007 12 08 21 08 50
>
> so that I can use gawk's mktime function.

[...]

A tested gawk script with the help of three general, short and reusable
helper functions:

# global variables: mon, perm

BEGIN {
assoc(mon, "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", " ")
split("3 2 1 4 5 6", perm, " ")
}

function get_time(t, dt, dtp) {
split(t, dt, "[/:]")
dt[2] = sprintf("%02d", mon[dt[2]])
permute(dtp, dt, perm)
return mktime(join(dtp, " "))
}



########################################
#####
# three reusable helper functions used above
########################################
#####

# associates words in s separated by strings matching
# dynamic regex re with their indices
# result: aout with aout[word] = index
function assoc(aout, s, re, ainv, i) {
split(s, ainv, re)
for (i in ainv) aout[ainv[i]] = i
}

# joins the strings a[i] (i=1,...,n) with the string sep in
# between them
function join(ain, sep, i, result, optsep) {
for (i = 1; i in ain; ++i) {
result = result optsep ain[i]
optsep = sep
}
return result
}

# permutes the array ain with the permutation perm into the array aout
# result: aout
function permute(aout, ain, perm, i) {
for (i in perm) aout[i] = ain[perm[i]]
}

--
Steffen
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com