| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC into a timestamp. How can we deal with this format [05/Jul/2008:00:27:35 +0000] ? If using gensub(/\[(..)\/(...)\/(....) ..) ..) ..)/, "\\3 \\2 \\1 \\4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35". Is there a way converting "Jul" to "07" so that we can blah("\\2") -> 07 ? |
|
#2
| |||
| |||
| On 7/4/2008 7:37 PM, dnlchen@gmail.com wrote: > We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC > into a timestamp. > > How can we deal with this format [05/Jul/2008:00:27:35 +0000] ? > > If using gensub(/\[(..)\/(...)\/(....) ..) ..) ..)/, "\\3 \\2 \\1 \> \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35". > > Is there a way converting "Jul" to "07" so that we can blah("\\2") -> > 07 ? Play with this: function cvttime(t, a) { split(t,a,"[/:]") match("JanFebMarAprMayJunJulAugSepOctNovDec",a[2]) a[2] = sprintf("%02d",(RSTART+2)/3) return( mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]) ) } BEGIN{ t1="01/Dec/2005:00:04:42" t2="01/Dec/2005:17:14:12" print cvttime(t2) - cvttime(t1) } Ed. |
|
#3
| |||
| |||
| dnlchen@gmail.com wrote: > We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC > into a timestamp. > > How can we deal with this format [05/Jul/2008:00:27:35 +0000] ? > > If using gensub(/\[(..)\/(...)\/(....) ..) ..) ..)/, "\\3 \\2 \\1 \> \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35". > > Is there a way converting "Jul" to "07" so that we can blah("\\2") -> > 07 ? $ cat date2mktime.awk #!/bin/sh TZ=UTC0 gawk ' BEGIN { tm = "[05/Jul/2008:00:27:35 +0000]" printf "%s ### %s\n", tm, strftime("%c", to_time(tm)) } function to_month_idx(month_abbr, months) { months = "JanFebMarAprMayJunJulAugSepOctNovDec" match(months, month_abbr) return (RSTART + 2) / 3 } function to_time(tm, dyn_re, month_abbr, format) { dyn_re = "^\\[(..)/(...)/(....) ..) ..) ..) \\+(....)\\]$"month_abbr = gensub(dyn_re, "\\2", 1, tm) format = gensub(dyn_re, "\\3 %02d \\1 \\4 \\5 \\6", 1, tm) return mktime(sprintf(format, to_month_idx(month_abbr))) }' $ chmod u+x date2mktime.awk $ ./date2mktime.awk [05/Jul/2008:00:27:35 +0000] ### Sat 05 Jul 2008 12:27:35 AM UTC $ -- Steffen |
|
#4
| |||
| |||
| On Jul 4, 7:07*pm, Ed Morton <mor...@lsupcaemnt.com> wrote: > On 7/4/2008 7:37 PM, dnlc...@gmail.com wrote: > > > We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC > > into a timestamp. > > > How can we deal with this format [05/Jul/2008:00:27:35 +0000] ? > > > If using gensub(/\[(..)\/(...)\/(....) ..) ..) ..)/, "\\3 \\2 \\1 \> > \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35". > > > Is there a way converting "Jul" to "07" so that we can blah("\\2") -> > > 07 ? > > Play with this: > > function cvttime(t, * * a) { > * * * * split(t,a,"[/:]") > * * * * match("JanFebMarAprMayJunJulAugSepOctNovDec",a[2]) > * * * * a[2] = sprintf("%02d",(RSTART+2)/3) > * * * * return( mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]) )} > > BEGIN{ > t1="01/Dec/2005:00:04:42" > t2="01/Dec/2005:17:14:12" > print cvttime(t2) - cvttime(t1) > > } > > * * * * Ed. This is awesome! When I use script languages to parse the logs, I find awk has both efficiency and flexibility. A perl module can finish this task, however it is not as efficient as awk. Now gawk has some new stuff such as array, built-in functions. Why don't we think about bringing in the modules? If so I think awk will get more popular. |
|
#5
| |||
| |||
| On Fri, 04 Jul 2008 17:37:29 -0700, dnlchen wrote: > We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC into > a timestamp. > > How can we deal with this format [05/Jul/2008:00:27:35 +0000] ? > > If using gensub(/\[(..)\/(...)\/(....) ..) ..) ..)/, "\\3 \\2 \\1 \ \4> \\5 \\6",""), the output will be "2008 Jul 05 00 27 35". > > Is there a way converting "Jul" to "07" so that we can blah("\\2") -> 07 ? I use a variation on the approach already given: MonthAbrvList = " JanFebMarAprMayJunJulAugSepOctNovDec" Month = sprintf( "%.2i",index( MonthAbrvList, Abrv )/3 ) Note that the two leading spaces on the list remove the need for +2 and that the number has leading zeros if single digit. I generally place the MonthAbrvList assignment in a BEGIN block and use the actual conversion where needed rather than making the whole thing a function. Also note that the two spaces can be anything that doesn't cause a false match, and it is probably better if they are something that clearly shows how many there are. -- T.E.D. (tdavis@mst.edu) MST (Missouri University of Science and Technology) used to be UMR (University of Missouri - Rolla). |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.