parsing and calculating difference.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing and calculating difference.
# 1  
Old 11-05-2008
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
# 2  
Old 11-05-2008
Please go to the FAQ and read the date arithmetic thread - look for the 'datecalc' script by Perderabo.
# 3  
Old 11-05-2008
As a start I want to get the time field into three different variables hours, minutes and seconds..

Any parsing script for that ??

Thanks
# 4  
Old 11-05-2008
Quote:
Originally Posted by meetmano143
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

Code:
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

Code:
#!/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:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Calculating Time difference Between two Rows in Linux

16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. working in media company on a project OS: RHEl7 tried command: awk 'function... (2 Replies)
Discussion started by: vivekn
2 Replies

3. Shell Programming and Scripting

Trouble calculating difference in number of days

Hi all, I have a requirement to calculate the difference of number of days of time stamp of a file and system date and if the difference is greater than 15 days it should prompt as previous month file otherwise current month file. Below is the code i used and it is working fine till now. (You... (2 Replies)
Discussion started by: Ravindra Swan
2 Replies

4. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

7. Shell Programming and Scripting

Calculating the difference between dates

Hello! i need to find files lower and bigger that one date i pass, i search in the man find, but i didn't find anything, the only that i find is the parameter -mtime, in this parameter i can pass a number of days, but i need to know the difference between dates, any built-in function for do... (15 Replies)
Discussion started by: claw82
15 Replies

8. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

9. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

10. Shell Programming and Scripting

calculating the time difference, when the script was executed and the currenent file

Hi, I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert. I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the... (7 Replies)
Discussion started by: velocitnitin
7 Replies
Login or Register to Ask a Question

Featured Tech Videos