Login or Register to Ask a Question and Join Our Community


Diff between two time in hours in last column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Diff between two time in hours in last column
# 1  
Old 11-23-2013
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.

Code:
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
# 2  
Old 11-23-2013
Try :

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

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 11-23-2013
Diff between two time in hours in last column

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?


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

Last edited by jim mcnamara; 11-23-2013 at 08:02 AM..
# 4  
Old 11-23-2013
Oh Sorry I didn't see use this, codetag please...

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

Code:
$ awk  '{s=$1 " "$2;t=$3" "$4;gsub(/\-|:/," ",s);gsub(/\-|:/," ",t); print $0, (mktime(s)-mktime(t))/3600}' file

Code:
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
Code:
$ awk  '{s=$1 " "$2;t=$3" "$4;gsub(/\-|:/," ",s);gsub(/\-|:/," ",t); print $0, int((mktime(s)-mktime(t))/3600)}' file

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


Last edited by Akshay Hegde; 11-23-2013 at 07:59 AM..
# 5  
Old 11-23-2013
Diff between two time in hours in last column

Dear Akahsy

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

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

Last edited by Franklin52; 11-23-2013 at 08:36 AM.. Reason: Please use code tags
# 6  
Old 11-23-2013
Quote:
Originally Posted by jaydeep_sadaria
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.
# 7  
Old 11-23-2013
Diff between two time in hours in last column

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.

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

Last edited by jaydeep_sadaria; 11-23-2013 at 08:31 AM.. Reason: another code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Shell Programming and Scripting

Add time hours

Gents, Is there the way to increase hours (+3) to the data with the format following. example JDhhmmss 335193508 input 335193508 335203508 335213508 335223508 335233508 output 335223508 335233508 336003508 336013508 (14 Replies)
Discussion started by: jiam912
14 Replies

3. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

4. Shell Programming and Scripting

awk : Search for text between two time frame (12 hours)

I have created the script to grep the errors from weblogic logs files and redirecting output to file.txt ...From file.txt I'm using awk command to collect the past 20 mins output...The script running from cron every 15 mins... The script working well... Now the challenges, I'm trying to use... (27 Replies)
Discussion started by: zenkarthi
27 Replies

5. Shell Programming and Scripting

Transpose Datefield from rows to column + Print time diff

Hi Experts, Can you please help me in transposing Datefield from rows to column and calculate the time difference for each of the Jobids: Input File: 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012 12:36:26,JOB_5350 Required Output:... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. UNIX for Dummies Questions & Answers

Execute crontab for every 4 hours and begin from current time

I want to add a crontab entry which should execute for every 4 hours and that 4 hours calculation should begin from the current time. Normally if I set the crontab entry like this, 00 */4 30 05 * root date >>/tmp/cronout The above will execute the date command for every 4 hours like... (7 Replies)
Discussion started by: Ganeshwari
7 Replies

7. Shell Programming and Scripting

Minues four hours from time

Hi, I am trying to design a script, which will monitor a log file, and it should dump all the rows that were logged between now and past four hours. The date format within the logs is "2/Jan/2011:03:13:27". So I am using below command to substract four hours from the current time : $... (8 Replies)
Discussion started by: john_prince
8 Replies

8. UNIX for Dummies Questions & Answers

Calculating the Hours between two time values

Dear Folks, I want to calculate the elapsed hours between two time columns. I am using timestampdiff method for the same. I am able to get the value. But facing an issue of decimal values. For example the elapsed hours between 09:00:00 and 20:30:00 is coming as 11 instead of 11.5. I am using below... (1 Reply)
Discussion started by: dinesh1985
1 Replies

9. 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

10. UNIX for Dummies Questions & Answers

capturing the time stamp to get hours

Hi All, I am working on a korn shell script. I have a problem.i am calculating the next 27 hours from the time stamp. say TSTAMP=20060724000000 if i add 27 hours to the above time stamp, i will get 20060724143000 this is the code: YEAR=`echo $TSTAMP | awk... (0 Replies)
Discussion started by: pavan_test
0 Replies
Login or Register to Ask a Question

Featured Tech Videos