Stephen Gran on 31 Jan 2009 10:30:47 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] date formatting in a shell script


On Sat, Jan 31, 2009 at 01:22:39PM -0500, Chad V said:
> On Sat, Jan 31, 2009 at 13:11, Chad V <csv@gamebox.net> wrote:
> > I'm working on a script that will figure out average requests per
> > second and the max RPS for a web server.  Unfortunately, the log file
> > doesn't put the date & time into a standard format.
> >
> > Here are 2 lines from the tab separated log file  (xxx where data was
> > but needs to stay anonymous such as IP, host, file, etc):
> >
> > INFO    20080501043217440       00000000000000000005    9569    34353   0       tcpConnection-17000-6   xxx     xxx     xxx     xxx     xxx     xxx     -       -       0       0       -       0       0       -       0       0               0       0       xxx
> > INFO    20080501043219725       00000000000000000006    7287    34353   0       tcpConnection-17000-7   xxx     xxx     xxx     xxx     xxx     xxx     -       -       0       0       -       0       0       -       0       0               0       0       xxx
> >
> >
> > The date looks like it includes:  YEAR MONTH DAY HOUR MINUTE SECOND
> > MILLISECOND (last 3 digits).
> >
> > I can use awk to grab the 2nd field and load that into a variable, but
> > I'm brain dead on where to go from there.  I want to end up with an
> > output such as 2008-05-01 04:32:17.440.   From there, I should be able
> > to perform math operations on the dates.
> >
> > I tried using the `date -d 20080501043217` without the ms field, but
> > it couldn't interpret it.
> >
> > Any ideas on how to get the string into the format I want?  I don't
> > want to do simple substitution (i.e. 2008  =  2008-, 05 = 05-) because
> > it should be generic enough to work on any date string in this screwy
> > format.
> >
> > Thanks for any help.
> >
> > Chad
> >
> 
> Nevermind.  I just discovered the "cut" command to break apart text
> strings of a known format.
> 
> I can cut chars 1-4, insert a -, then cut chars 5-6, insert another - and so on.

Simplest might be something like:

sgran@cmburns:~$ cat log
INFO    20080501043217440       00000000000000000005    9569    34353   0       tcpConnection-17000-6   xxx     xxx     xxx     xxx     xxx     xxx     -       -       0       0       -       0       0       -       0       0               0       0       xxx
INFO    20080501043219725       00000000000000000006    7287    34353   0       tcpConnection-17000-7   xxx     xxx     xxx     xxx     xxx     xxx     -       -       0       0       -       0       0       -       0       0               0       0       xxx

sgran@cmburns:~$ awk '/INFO/ {print $2}' log | sed -e 's/^\([[:digit:]]\{4\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{3\}\)$/\1-\2-\3 \4:\5:\6.\7/'
2008-05-01 04:32:17.440
2008-05-01 04:32:19.725

But perhaps not all that readable.
-- 
 --------------------------------------------------------------------------
|  Stephen Gran                  | Do you realize how many holes there     |
|  steve@lobefin.net             | could be if people would just take the  |
|  http://www.lobefin.net/~steve | time to take the dirt out of them?      |
 --------------------------------------------------------------------------

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug