Thursday, August 04, 2005

Parsing a date field

Parsing a date field.

Subject: Parsing a date field.

Hi,

I want to be able to extract selected lines from a logfile, where each
line is tagged with today's date.

e.g

Mon Jul 25 13:58:15 2005: Loading file ...
Mon Jul 25 13:58:16 2005: Deleting all data from tblCusipMap

Say that today is Mon Jul 25 2005. How can I extract all of today's
lines from the file (ignoring the timestamp, but including the year)
without getting too messy?

Cheers!

Reply


John L Jul 25, 4:37 am

You need to grep today's date from the file, and
you can get today's date using the date command.

Because you need the date in two sections (the year is
separate) you could run the date command twice but that
will fail around midnight on New Year's Eve so just run
it once and assign the result to shell variables.

set -- `date +'%a %b %d %Y'`
grep "$1 $2 $3 [0-9:]* $4" logfile

For ease of future maintenance, consider using meaningful
variable names.

--
John.

R
Oops. You should add a ^ to anchor the pattern to the start of
each line, just to be on the safe side.

set -- `date +'%a %b %d %Y'`
grep "^$1 $2 $3 [0-9:]* $4" logfile

--
John.

Reply


Chris F.A. Johnson

grep "`date "+^%a %b %d [0-9][0-9]:[0-9][0-9]:[0-9][0-9] %Y:"`" LOGFILE


William James

ruby -nae 'BEGIN{$a=Time.now.to_s.+(":").split;$a.slice!(3,4)};print if
$a==$F.values_at(0,1,2,4)'

gawk '$1$2$3$5==strftime("%a%b%d%Y:")' file


John W. Krahn J
perl -ne'
BEGIN { ( $today = localtime ) =~ s/\d+:\d+:\d+/\\d+:\\d+:\\d+/ }
print if /^$today/o
' logfile

0 Comments:

Post a Comment

<< Home