Parsing log files, displaying logs between specific dates

Sorry, couldn't really think of a simple subject/title.

So, I have a log file, and the dates are displayed like so:

2009-03-05 02:49:44

So the first and second field are the date/time. I can change them into a unix timestamp easily with:

date -d "2009-03-05 02:49:44" +%s

However, trying to do that is not as easy as I thought. I wanted to get all logs between X and Y time (lets say for a few hours on Z day). To make this easily reusable, I would rather do it this way.

I have tried to use awk with the system() function, but maybe I did not use it correctly..

awk '{ system("date -d " $1,$2 " +%s"); }'

Of course, I have tried other varieties of this, but to no avail. I have thought of possibly doing it other ways, but I am not even sure what will work now.. Anyone have any suggestions?

assume X and Y are already variables that are in epoch seconds ie +%s format.

while read t1 t2 restofline
do
      t=$(date -d "$t1 $t2" +%s)
      if [[ $(( $t >= $X )) -eq 1  &&  $(( $t <= $Y )) -eq 1 ]] ; then
      	echo "$t1 $t2 $restofline"
      fi
done < logfile > resultfile

can anybody me on this its urgent

i have a script

echo "enter date:\c"
read date

if [ -z "$date" ]
then
echo "Please enter the date"
fi

now when i run the script and enter the date in say

05032009

then i want to print o/p as

05 March 2009

how do i do this

thanks,
haris

With gawk time and string functions:
input file:

127.0.0.1 - - 2009-03-05 17:17:25 "GET / HTTP/1.1" 304 - "-"
awk 'mktime(gensub(/-|:/, " ", "g", $4" "$5)) > 1236294000 ' file

This will print lines with date > 2009-03-06. Just add a condition for the lower boundary and you are done.

The gensub() string function replaces the - and : characters by space as the mktime() function only accepts the YYYY MM DD HH MM SS format.

Check if your awk version supports these functions.

is there any other way to get
05032009
as
05 March 2009

bcoz i didnt got the above code exactly

thanks
haris

@Haris, this is another problem than the OP. But however, here is a possible solution depending on your distribution. This one is using GNU coreutils date command.

dte=05032009
dteIso=$(echo $dte | sed 's/\(..\)\(..\)\(....\)/\3\2\1/')
LC_ALL=C date -d $dteIso +'%d %B %Y'

hi i have tried your previous code but it didnt wrk
thats the mean reason for previous post

so could u please help me with this
i just want to know how to convert

figure to text

if u can please explain me with example
as i m quite new to unix scripting

thanks
haris

What is not working? sed or the date command?

What is your OS?

i m on HP-UX

and it gives me

date: illegal option -- y
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]

in this line

LC_ALL=C date -d $dteIso +'%d %B %Y'

i hane understood this

dteIso=$(echo $dte | sed 's/\(..\)\(..\)\(....\)/\3\2\1/') and it gives o/p as

20090305

but now we have to convert this into

5 Mar 2009

please respond

thanks
haris

Using bash or ksh

#!/bin/bash
dte=05032009
d=${dte:0:2}
m=${dte:2:2}
y=${dte:4:4}

arrMonth=(January February March April ...)

echo $d ${arrMonth[$m-1]} $y

it gives an error

d=${dte:0:2}: bad substitution

thanks,
haris

Depends on the shell you use. What's that shell?

I don't have a HP-UX machine to help you further.

ripat,

pls dont put such

quotes

@ haris this is the third time you post the very same question on someone else's post. I have replied to your problem here:
Parsing log files, displaying logs between specific dates

first see to it that ur code works properly on HP-UX

i m not a mad guy nor i get pleasure by asking the same question
i want to be a part of this sight so dont do such things which might cost me
this is bcoz of u

Reason: Thread hijacking
-------

-------

This infraction is worth 2 point(s) and may result in restricted access until it expires. Serious infractions will never expire.

so pls i urge u not u put such responses if u cant help me or provide proper sol its ok

haris,
Please read the rules of these Forums you agreed to previously.

vgersh99,

sorry i will keep this in mind.but dont know how to post a new thread from starting
bcoz i knw only way from where u can post a thread that is at the bottom of already existing post i.e through post reply.

could u please let me knw the exact path from where i can post a new thread from starting as i m very new to this forum n never posted any thread from starting.

so i desperately need some help.

thanks
haris

Select an appropriate forum from the list and click 'New thread' in the upper left corner of the screen.

haris,

I urge you to read the rules and follow them.

If you continue to write "pls" and not "please" "u" and not "you" etc you will be banned.

Understand?

The message you have entered is too short. Please lengthen your message to at least 5 characters.

It gives me this error when I try to post a new thread.

please tell me how to avoid this

thanks in advance,
haris