Time difference in minutes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time difference in minutes
# 1  
Old 10-16-2014
Time difference in minutes

Hi Folks,

I have a text file that has only time in the format HH:MM:SS like seen below.

21:36:17
23:52:08

I need to find the difference in minutes alone from this text file so the result would be 136.

Thanks
Jay
# 2  
Old 10-16-2014
Quote:
Originally Posted by jayadanabalan
Hi Folks,

I have a text file that has only time in the format HH:MM:SS like seen below.

21:36:17
23:52:08

I need to find the difference in minutes alone from this text file so the result would be 136.

Thanks
Jay
Hello jay,

Assuming your file is in following format. Then following may help.
File:
Code:
cat time_diff
21:36:17        23:52:08

Code:
awk 'function time_details(A){split(A,X,":"); TOT=X[1] * 3600 + X[2] * 60 + X[3]; return TOT} {print time_details($2) - time_details($1)}'  time_diff


Thanks,
R. Singh
# 3  
Old 10-16-2014
# 4  
Old 10-16-2014
Hi,

Thanks much for the reply.

I have both the times line after line. The code that you gave me will work only if the time is in the same line i believe.

Any way we could find the difference from this 2 lines ?

Thanks
Jay
# 5  
Old 10-16-2014
@RavinderSingh: What about date changes? Also the time should be in minutes.
@Jay, could you post what tried yourself?

Last edited by Scrutinizer; 10-16-2014 at 05:06 AM..
# 6  
Old 10-16-2014
Hi,

I can fetch the time and date in the format seen below.

10/15/14 21:36:17
10/15/14 23:52:08

Now i need to find the difference in minutes or seconds.

I get the following error when i try to run the command given by R.Singh.

awk: syntax error near line 1
awk: bailing out near line 1

Thanks
Jay
# 7  
Old 10-16-2014
Quote:
Originally Posted by jayadanabalan
Hi Folks,

I have a text file that has only time in the format HH:MM:SS like seen below.

21:36:17
23:52:08

I need to find the difference in minutes alone from this text file so the result would be 136.

Thanks
Jay
Quote:
Originally Posted by jayadanabalan
Hi,

Thanks much for the reply.

I have both the times line after line. The code that you gave me will work only if the time is in the same line i believe.

Any way we could find the difference from this 2 lines ?

Thanks
Jay
Sample input line by line start time, endtime, and so on
Code:
[akshay@nio tmp]$ cat file
21:36:17
23:52:08
20:26:17
22:32:08

Code executed
Code:
[akshay@nio tmp]$ cat s.awk
function dfor(time, a)
{ 
	  split(time, a, /:/); 
	  return 3600*a[1] + 60*a[2] + a[3] 
}
function hms(s, h,m)
{
	  h=int(s/3600);
	  s=s-(h*3600);
	  m=int(s/60);
	  s=s-(m*60);
	  return sprintf("%02d:%02d:%02d", h, m, s);
}


FNR==1{ print "Start","END","Seconds","Minutes","HH:MM:SS" }

{ 
	s[v = (FNR%2)?"s":"e" ] = $1	
	if(v=="e")
	{
		seconds = dfor(s["e"])-dfor(s["s"])
		print s["s"],s["e"],seconds,seconds/60,hms(seconds)
	}
}


Output

Code:
[akshay@nio tmp]$ awk -f s.awk file
Start END Seconds Minutes HH:MM:SS
21:36:17 23:52:08 8151 135.85 02:15:51
20:26:17 22:32:08 7551 125.85 02:05:51

This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. Shell Programming and Scripting

How to get a time minus 60 minutes?

Hello, date --date '-60 min ago' +'%Y-%m-%d %H:%M:%S,%3N' Above command gives the date and time minus 60 minutes but the problem i am facing is, i do not want to hardcode the value 60 it is stored in a variable var=60 now if i run below command , i get error date --date '-$var min... (3 Replies)
Discussion started by: Ramneekgupta91
3 Replies

3. Shell Programming and Scripting

grep the time within given minutes

Mar 26 15:25:11 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 26 15:28:52 : jdoe : 3 incorrect password attempts ; TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 25 12:23:07 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; ... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

4. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

5. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

6. Shell Programming and Scripting

Process Time in hours or minutes only

Hi i want to print the time of a process in hours only..(or) in minutes only.Is there anyway to print the process such like that when i give the commnand like following #ps -eo pid,time PID TIME 412 01:49:32 481 00:03 it shows in HH:MM:SS format: Could anyone... (1 Reply)
Discussion started by: srikanthg
1 Replies

7. Shell Programming and Scripting

how to find the time before 30 minutes

Hi All, I want to find out the time before 30 minutes. I am able to do with in hours limit. date Fri Aug 21 06:50:00 BST 2009 TZ=CST+1 date Fri Aug 21 04:50:02 CST 2009 Can any one please help me (6 Replies)
Discussion started by: vikash_k
6 Replies

8. Shell Programming and Scripting

Difference in day-hours-minutes-seconds format

Hi experts, I am reading two log files and passing dates as output to a txt file. Code is given below: echo "Start Time:" >> Report.txt cat start.log | while read LINE1 do echo $DATE1 >> Report.txt done echo "End Time:" >> Report.txt cat end.log | while read LINE2 ... (7 Replies)
Discussion started by: Sreejith_VK
7 Replies

9. UNIX for Advanced & Expert Users

Adding # minutes to current time...

Hi all, Looking for a way to add lets say 10 minutes to the current time output should look like 7:15 AM or 7:15 PM. I know that gdate could do this for me but unfortunately its not available on the system I'm working on. So if any one know any way I can accomplish this using the date command it... (7 Replies)
Discussion started by: gptavares
7 Replies

10. Shell Programming and Scripting

how to display time in minutes n seconds...

Hi all, may i know how to display time in minutes and seconds(may be milliseconds and even smaller that ) in shell scripts.... (1 Reply)
Discussion started by: santy
1 Replies
Login or Register to Ask a Question