Diff between two time in hours in last column

Dear All

I want to diff between two time(FIRST 4 COLUMN) in hours in last column. Kindly help me for same.

2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG
 2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG
 2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG
 2013-11-23 15:51:23 2013-11-22 13:17:50 VAK001 NG
 2013-11-23 15:51:23 2013-11-22 14:02:42 ANK020 SAU
 2013-11-23 15:51:23 2013-11-23 12:59:28 SUR119 SAU 

Regards
Jaydeep

Try :

$ awk '{split($2,S,":");split($4,E,":");print $0, ((S[1]*3600+S[2]*60+S[3])-(E[1]*3600+E[2]*60+E[3]))/3600}' file
2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 3.52139
2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 3.52083
2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 2.55944
2013-11-23 15:51:23 2013-11-22 13:17:50 VAK001 NG 2.55917
2013-11-23 15:51:23 2013-11-22 14:02:42 ANK020 SAU 1.81139
2013-11-23 15:51:23 2013-11-23 12:59:28 SUR119 SAU 2.86528
1 Like

Dear Akshay

That nice. Only one part remaining is considering of date also. 2 line had a previous day date. In that case what can b e solution?

2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 3.52139
2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 3.52083
2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 2.55944
2013-11-23 15:51:23 2013-11-22 13:17:50 VAK001 NG 2.55917
2013-11-23 15:51:23 2013-11-22 14:02:42 ANK020 SAU 1.81139
2013-11-23 15:51:23 2013-11-23 12:59:28 SUR119 SAU 2.86528

Regards
Jaydeep

Oh Sorry I didn't see use this, codetag please...

while read line; do
    IFS=' '; read -r  s1 s2 s3 s4 _ _ <<< $line
    echo  $line $((  ( $(date --date="$s1 $s2" +%s) - $(date --date="$s3 $s4" +%s) )/3600 ))
done <"file"

Just hours in bash

$ bash diff.sh 
2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 3
2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 3
2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 26
2013-11-23 15:51:23 2013-11-22 13:17:50 VAK001 NG 26
2013-11-23 15:51:23 2013-11-22 14:02:42 ANK020 SAU 25
2013-11-23 15:51:23 2013-11-23 12:59:28 SUR119 SAU 2

OR

$ awk  '{s=$1 " "$2;t=$3" "$4;gsub(/\-|:/," ",s);gsub(/\-|:/," ",t); print $0, (mktime(s)-mktime(t))/3600}' file
2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 3.52139
2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 3.52083
2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 26.5594
2013-11-23 15:51:23 2013-11-22 13:17:50 VAK001 NG 26.5592
2013-11-23 15:51:23 2013-11-22 14:02:42 ANK020 SAU 25.8114
2013-11-23 15:51:23 2013-11-23 12:59:28 SUR119 SAU 2.86528

to round use int function like this

$ awk  '{s=$1 " "$2;t=$3" "$4;gsub(/\-|:/," ",s);gsub(/\-|:/," ",t); print $0, int((mktime(s)-mktime(t))/3600)}' file
2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 3
2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 3
2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 26
2013-11-23 15:51:23 2013-11-22 13:17:50 VAK001 NG 26
2013-11-23 15:51:23 2013-11-22 14:02:42 ANK020 SAU 25
2013-11-23 15:51:23 2013-11-23 12:59:28 SUR119 SAU 2

Dear Akahsy

For while loop tag following error occur. Help. Last error but I cant find.

sh diff.sh
diff.sh: syntax error at line 2: `<' unexpected
atisdosh@gumas1n:~/PROJECT/Format_SMS$cat diff.sh
while read line; do
    IFS=' '; read -r  s1 s2 s3 s4  <<< $line
    echo  $line $((  ( $(date --date="$s1 $s2" +%s) - $(date --date="$s3 $s4" +%s) )/3600 ))
done < ip.txt
You have new mail in /var/mail/atisdosh

For awk code following error occur:

awk  '{s=$1 " "$2;t=$3" "$4;gsub(/\-|:/," ",s);gsub(/\-|:/," ",t); print $0, int((mktime(s)-mktime(t))/3600)}' ip.txt
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

Regards
Jaydeep

---------- Post updated at 05:47 PM ---------- Previous update was at 05:40 PM ----------

Dear Akshay

gsub is not working. substr and split is working. What can be code for same.

Regards
Jaydeep

which OS ? if Solaris/Sun OS then use nawk

I am very sad that though you are member since 2007, you have been very consistent in not using codetag. and secondly you have not copied the code properly.

Dear Akahay..

Sry. I was in hurry. nwak gives error of mktime is undefined function. What can be done? Can you guide me what is wrong in while loop code.

nawk '{s=$1 " "$2;t=$3" "$4;gsub(/\-|:/," ",s);gsub(/\-|:/," ",t); print $0, (mktime(s)-mktime(t))/3600}' ip.txt
2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG nawk: calling undefined function mktime
 input record number 1, file ip.txt
 source line number 1 

Regards
Jaydeep

I am basically gawk user I heard that mktime() is NOT a standard awk feature. It is a gawk feature, I am not sure about nawk .But still you can use bash solution which I have given or try with these /usr/xpg4/bin/awk or /usr/xpg6/bin/awk

following is working I tested with your sample data

while read line; do
    IFS=' '; read -r  s1 s2 s3 s4 _ _ <<< $line
    echo  $line $((  ( $(date --date="$s1 $s2" +%s) - $(date --date="$s3 $s4" +%s) )/3600 ))
done <"file"

Dear Akshay

Thanks. Can u plz guide me for below help.

sh diff.sh
diff.sh: syntax error at line 2: `<' unexpected

Regards
Jaydeep

Try

$ bash scriptname.sh

OR

$ chmod +x  scriptname.sh 
$ ./scriptname.sh