i grepped the time stamp in a file as given below
now i need to calculate time difference
file data:
18:29:10
22:15:50
i grepped the time stamp in a file as given below
now i need to calculate time difference
file data:
18:29:10
22:15:50
Hello vivekn,
Could you please let me know if following helps you on same.
awk -F":" 'FNR==1{split($0,a,":");sec1=a[1]*3600+a[2]*60+a[3]} FNR==2{split($0,b,":");sec2=b[1]*3600+b[2]*60+b[3];print (sec2-sec1)/3600}' Input_file
Output will be as follows.
3.77778
Thanks,
R. Singh
thanks, I need output in following format
03:46:40
Hello vivekn,
Could you please try following and let me know if this helps.
awk -F":" 'FNR==1{split($0,a,":");sec1=a[1]*3600+a[2]*60+a[3]} FNR==2{split($0,b,":");sec2=b[1]*3600+b[2]*60+b[3];val=sec2-sec1;ho=int(val/3600);val=val%3600;mi=sprintf("%02d",(val/60));val=val%60;;print ho":"mi":"val}' Input_file
Output will be as follows.
3:46:40
EDIT: Adding a non-one liner form of solution too now.
awk -F":" '
FNR==1{
split($0,a,":");
sec1=a[1]*3600+a[2]*60+a[3]
}
FNR==2{
split($0,b,":");
sec2=b[1]*3600+b[2]*60+b[3];
val=sec2-sec1;
ho=int(val/3600);
val=val%3600;
mi=sprintf("%02d",(val/60));
val=val%60;
print ho":"mi":"val
}
' Input_file
Thanks,
R. Singh
Great it worked, Thanks alot
Hi.
For one of a set of utilities for dealing with units date/time:
dateutils.ddiff -f "%H:%M:%S" 18:29:10 22:15:50
prodcuing:
3:46:40
On a system like:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
Some details about ddiff
:
dateutils.ddiff Compute duration from DATE/TIME (the reference date/ti... (man)
Path : /usr/bin/dateutils.ddiff
Package : dateutils
Home : http://www.fresse.org/dateutils
Version : 0.3.1
Type : ELF 64-bit LSB shared object, x86-64, version 1 ( ...)
Help : probably available with -h,--help
Home : https://github.com/hroptatyr/dateutils (doc)
Best wishes ... cheers, drl