Computing.Net > Forums > Linux > Scripting diffrence of two dates

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Scripting diffrence of two dates

Reply to Message Icon

Name: abhargav
Date: April 9, 2005 at 08:46:47 Pacific
OS: Linux
CPU/Ram: 256SDRAM
Comment:

hi
my question is abt scripting.
i want to find the diffrence in two dates.
dates are in /var/log/messages

Mar 27 05:48:02 abhargav sshd[1151]:
Apr 2 15:31:17 abhargav sshd[1151]:

now i can get the dates in strings using awk
but doesnt solve my purpose.i hav to find the diffrence between these two dates.is there any way that i can convert into milliseconds and then find out the diffrence

in simple words,i hav to find the diffrence of two dates.any ideas will be appreciated .
thanx
abhishek


hi
my question is abt scripting.
i want to find the diffrence in two dates.
dates are in /var/log/messages

Mar 27 05:48:02 abhargav sshd[1151]:
Apr 2 15:31:17 abhargav sshd[1151]:

no


hi
my question is abt scripting.
i want to find the diffrence in two dates.
dates are in /var/log/messages

Mar 27 05:48:02 abhargav sshd[1151]:
Apr 2 15:31:17 abhargav sshd[1151]:

no



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: April 9, 2005 at 10:28:18 Pacific
Reply:

You can do it with an awk command. Read the info pages - specifically the awk mktime() function.


0

Response Number 2
Name: abhargav
Date: April 9, 2005 at 17:18:18 Pacific
Reply:

hi
it seems to me that mktime() needs a c library
now i m using bourne shell scripting #!/bin/sh
i hav googled it a lot but cant find out the
exactly how to use mktime(awk scripting using sh) in converting it into time stamp.

2) just like ansi c they hav this function of localtime and systime....which returns in milliseconds.....can we do this in bourne shell scripting.

plz give an example

i will be very grateful to u.
thanx
abhishek


0

Response Number 3
Name: Wolfbone
Date: April 9, 2005 at 23:32:06 Pacific
Reply:

Of course awk needs a C library! I'm not talking about the C library mktime() but awk's mktime(). Have you read the awk info page entry for mktime()?

Since you're already using awk you don't need an example, you just need to feed the strings you extract from the log file into awk's mktime() (with a little reformatting first).

Alternatively you could use some combination of sed/grep/shell and the 'date' command, which will take various different formats.

This is a very simple job and there are many tools that can help you do it so you should just read the info/man pages and try them out.

BTW - you won't get milliseconds with either awk's mktime() or using the 'date' command but since you don't need better than seconds resolution anyway, it doesn't really matter does it?



0

Response Number 4
Name: abhargav
Date: April 11, 2005 at 09:07:27 Pacific
Reply:

Dear Wolfbone
Thanx a lot for your replies.but as i have written i would be needing an example of mktime or the tools .i hav read the man pages,but i cant implement it.it gives me an syntax error.

then at the same time, the assignment that i m doin requires shell scripting without using any c library.

plz if possible give me an example of what u r talking about
thanks a ton
abhishek

hi
my question is abt scripting.
i want to find the diffrence in two dates.
dates are in /var/log/messages

Mar 27 05:48:02 abhargav sshd[1151]:
Apr 2 15:31:17 abhargav sshd[1151]:

no


0

Response Number 5
Name: Wolfbone
Date: April 12, 2005 at 16:32:07 Pacific
Reply:

If you are having trouble using awk or understanding it's info (not man) pages then use the 'date' command and shell arithmetic:

echo $(( $(date -d 'Apr 2 2005 15:31:17' '+%s') - $(date -d 'Mar 27 2005 05:48:02' '+%s') ))

This is in a bash shell and it is trivial to replace the two date strings with variables set to strings extracted from the log file.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Scripting diffrence of two dates

diffrence of two dates www.computing.net/answers/linux/diffrence-of-two-dates/27635.html

How to support mouse under console? www.computing.net/answers/linux/how-to-support-mouse-under-console/27587.html

Thinking of RH 9? Then Read This. www.computing.net/answers/linux/thinking-of-rh-9-then-read-this/25280.html