Shell script - getting Time difference using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell script - getting Time difference using awk
# 1  
Old 10-23-2013
RedHat Shell script - getting Time difference using awk

Hi..I have the data in a file like in this format, and I need the output time difference in seconds by using awk command. Start date/time and end date/time given in column 2,3 & 4,5. Please assist how to write shell script.
Code:
File1.txt
JOB1 10/09/2013  17:42:16 10/09/2013  17:43:46 SU 6202685/1
JOB2 10/09/2013  18:05:17 10/09/2013  18:06:30 SU 6202787/1
JOB3 10/10/2013  00:05:19 10/10/2013  00:06:42 SU 6204211/1
JOB4 10/10/2013  03:05:15 10/10/2013  03:06:19 SU 6204847/1
JOB5 10/16/2013  06:05:13 10/15/2013  20:05:45 SU 3142148/1

Moderator's Comments:
Mod Comment Please use mext time code tags for your data and code, thanks
# 2  
Old 10-23-2013
Code:
#! /bin/bash

while read a d1 t1 d2 t2 b
do
    s=$(date -d "$d1 $t1" +%s)
    e=$(date -d "$d2 $t2" +%s)
    if [ $e -ge $s ]
    then
        echo "$(( e - s ))s"
    else
        echo "$(( s - e ))s"
    fi
done < file

# 3  
Old 10-23-2013
try perl:
Code:
perl -e '
use Time::Local;
while (<>) {
   print $_;
   @a=split(/  */, $_);
   @d1=split(/[\/:]/, @a[1]. ":" . @a[2]);
   @d2=split(/[\/:]/, @a[3]. ":" . @a[4]);
   $t1=timegm(@d1[5],@d1[4],@d1[3],@d1[1],@d1[0],@d1[2]);
   $t2=timegm(@d2[5],@d2[4],@d2[3],@d2[1],@d2[0],@d2[2]);
   $td=$t2 - $t1;
   print $td . "\n";
}' infile

# 4  
Old 10-23-2013
What's your system? awk has date features on a few systems but not on others.
# 5  
Old 10-24-2013
Using SunOS... Great thanks... the first script working fine.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to find time difference between HTTP PUT and HTTP DELETE requests in access.log

Hi, I'm trying to write a script to determine the time gap between HTTP PUT and HTTP DELETE requests in the HTTP Servers access log. Normally client will do HTTP PUT to push content e.g. file_1.txt and 21 seconds later it will do HTTP DELETE, but sometimes the time varies causing some issues... (3 Replies)
Discussion started by: Juha
3 Replies

2. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

3. Shell Programming and Scripting

Date / Time difference in shell script

Request ID GMDCOMTM GMDRRSTIME GMDRESTIME <36812986> : : :I want to display the date -time difference in other fields. Above I have given for only 1 record. I want to calculate for all the records. (GMCOMTM - GMDRRSTM) ,(GMDRRSTM-GMDRESTM) and... (5 Replies)
Discussion started by: ghosh_tanmoy
5 Replies

4. Shell Programming and Scripting

Date / Time difference in shell script

================================================================================ Request ID GMDCOM TIME GMDRRS TIME COM-RRS ================================================================================ <36812974> Tue Oct 1 13:32:40 2013 Tue Oct 1 20:36:42 2013... (1 Reply)
Discussion started by: ghosh_tanmoy
1 Replies

5. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

6. Shell Programming and Scripting

Difference Time Unix Shell

I have 2 variables MTIME="Jan_2_2012_23:55:49" SCH_TIME="Jan_03_2012_00:32:28" I want to find the time taken (in seconds) between MTIME and SCH_TIME. Is there any way by which this can be done in Unix Shell Script? (4 Replies)
Discussion started by: ankitncr
4 Replies

7. Shell Programming and Scripting

Log Analysis with AWK with Time difference

I would like to write a shell script that calculated the time difference bettween the log entries. If the time difference is higher as 200 sec. print the complette lines out. My Problem is, i am unable to jump in the next line and calculate the time difference. Thank you for your Help. ... (5 Replies)
Discussion started by: fabian3010
5 Replies

8. Shell Programming and Scripting

Script to display time difference..

Hi i've written a script which reads last two line of the log file from N number of servers and send the mail by redirecting to a particular log file. And the two lines is displayed below. Oracle Q03 Begin Hot BACKUP Time: 07/23/08 18:35:46 Oracle Q03 End Hot BACKUP Time: 07/24/08 14:18:15... (1 Reply)
Discussion started by: suri.tyson
1 Replies

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

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question