parsing and calculating difference.

Hi,

I have a file with the contents as following

Access Time: Thu Nov 6 16:43:45 2008
Modify Time: Thu Nov 6 16:43:45 2008
Change Time: Thu Nov 6 16:43:45 2008
Access Time: Thu Nov 6 16:43:02 2008
Modify Time: Thu Nov 6 16:44:01 2008
Change Time: Thu Nov 6 16:44:01 2008

I need to calculate the difference between two Access times. You can ignore the year and date.

just the difference between time stamps will do

Any ideas ??

Thanks,
Manohar

Please go to the FAQ and read the date arithmetic thread - look for the 'datecalc' script by Perderabo.

As a start I want to get the time field into three different variables hours, minutes and seconds..

Any parsing script for that ??

Thanks

Hello!

Here's a suggestion, You can of course manipulate the read command to extract hours and minutes, etc if You like.

Assuming You have a file date.log, like You described, and it really follows a predictable format, like

Access Time: Thu Nov 6 16:43:45 2008
Modify Time: Thu Nov 6 16:43:45 2008
Change Time: Thu Nov 6 16:43:45 2008
Access Time: Thu Nov 6 16:43:52 2008
Modify Time: Thu Nov 6 16:44:01 2008
Change Time: Thu Nov 6 16:44:01 2008
Access Time: Thu Nov 6 16:48:45 2008
Modify Time: Thu Nov 6 16:48:45 2008
Change Time: Thu Nov 6 16:48:45 2008
Access Time: Thu Nov 6 17:43:45 2008
Modify Time: Thu Nov 6 17:43:45 2008
Change Time: Thu Nov 6 17:43:45 2008

Then the following snippet

#!/bin/bash
prevatime=0
echo Previous Access time is 1970"!"
while read type dummy datestring
do 
	case $type in
	Access) curatime=$(date +%s -d "$datestring");
		echo Difference from prev access time is \
		$(((curatime-prevatime)/60)) minutes and \
		$(((curatime-prevatime)%60)) seconds;;
	Modify) ;;
	Change)	prevatime=$curatime;;
	esac
done < date.log

will give You an output like this:

Previous Access time is 1970!
Difference from prev access time is 20433103 minutes and 45 seconds
Difference from prev access time is 0 minutes and 7 seconds
Difference from prev access time is 4 minutes and 53 seconds
Difference from prev access time is 55 minutes and 0 seconds

Maybe something to start with?

Best regards,
Lakris