Date / Time difference in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date / Time difference in shell script
# 1  
Old 11-08-2013
Date / Time difference in shell script

Code:
Request ID      GMDCOMTM                     GMDRRSTIME                 GMDRESTIME     


<36812986> [Tue Oct 1 13:37:53 2013]: [Tue Oct 1 20:29:29 2013]: [Tue Oct 1 13:43:24 2013]:

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 (GMDCOMTM-GMDRESTM).

I want to achieve it without using AWK or SED.

Last edited by ghosh_tanmoy; 11-08-2013 at 06:20 AM.. Reason: Requirement changed.
# 2  
Old 11-08-2013
Could this help you ?
Code:
perl -nle 'if($.>3){$,="#"; print /\[(.+?)\]/g}' filename |
while read line
do 
IFS=#
set -- $line;
sec1=`date -d "$1" '+%s'`;
sec2=`date -d "$2" '+%s'`;
sec3=`date -d "$3" '+%s'`;
echo "(GMCOMTM - GMDRRSTM) diff in seconds: "$(($sec1 - $sec2));
echo "(GMDRRSTM-GMDRESTM) diff in seconds: "$(($sec2-$sec3));
echo "(GMDCOMTM-GMDRESTM) diff in seconds: " $(($sec1-$sec3));
done

# 3  
Old 11-08-2013
Requirement changed a bit.

I want it using simple BASH shell script.
# 4  
Old 11-08-2013
@ ghosh_tanmoy what you have tried so far ? show us your script we shall help you.
# 5  
Old 11-08-2013
Sample script

Code:
LineNo=0
echo "========================================================================================================================================="
echo "Request ID         GMDCOM TIME               GMDRRS TIME               GMDRES TIME         COM-RRS            RRS-RES        COM-RES"
echo "========================================================================================================================================="
while read -r Line
do
LineNo=`expr $LineNo + 1`
if [ $LineNo -gt 3 ]
then
ReqID=`echo $Line | cut -f 9 -d " "`
LineFromRRS=`grep $ReqID ./GMDRRS*`
LineFromRES=`grep $ReqID ./GMDRES*`
GMDCOMTM=`echo $Line | cut -f 2-6 -d " "`
GMDRRSTM=`echo $LineFromRRS | cut -f 2-6 -d " "`
GMDRESTM=`echo $LineFromRES | cut -f 2-6 -d " "`
Date1=`date "$GMDCOMTM" +%s`
Date2=`date "$GMDRRSTM" +%s`
Diff=`expr $Date1 - $Date2`
echo $ReqID $GMDCOMTM $GMDRRSTM $GMDRESTM $Diff
fi
done < $1

The highlighted portion in red is having the problem.
# 6  
Old 11-11-2013
Çould this help you ?
Code:
LineNo=0
echo "========================================================================================================================================="
echo "Request ID         GMDCOM TIME               GMDRRS TIME               GMDRES TIME         COM-RRS            RRS-RES        COM-RES"
echo "========================================================================================================================================="
while read -r Line
do
LineNo=`expr $LineNo + 1`
if [ $LineNo -gt 3 ]
then
ReqID=`echo $Line | cut -f 9 -d " "`
LineFromRRS=`grep $ReqID ./GMDRRS*`
LineFromRES=`grep $ReqID ./GMDRES*`
GMDCOMTM_Temp=`echo $Line | cut -f 2-6 -d " "`
GMDRRSTM_Temp=`echo $LineFromRRS | cut -f 2-6 -d " "`
GMDRESTM_Temp=`echo $LineFromRES | cut -f 2-6 -d " "`

GMDCOMTM=$(date -d "$(echo ${GMDCOMTM_Temp:1:`expr ${#GMDCOMTM_Temp}-3`})" '+%s')
GMDRRSTM=$(date -d "$(echo ${GMDRRSTM_Temp:1:`expr ${#GMDRRSTM_Temp}-3`})" '+%s')
GMDRRSTM=$(date -d "$(echo ${GMDRESTM_Temp:1:`expr ${#GMDRESTM_Temp}-3`})" '+%s')

Diff=`expr $GMDCOMTM - $GMDRRSTM`
echo "$ReqID $GMDCOMTM $GMDRRSTM $GMDRESTM $Diff"
fi
done < $1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In HP-UX how to find the date time difference ?

Hello, In HP-UX how to find the date time difference ? Start time: 28-APR-2019 21:36:01 End time : 29-APR-2019 00:36:04 ---------------------- Difference is ---------------------- Much appreciate any pointer or view on this. ... (3 Replies)
Discussion started by: Siva SQL
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 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

4. UNIX for Dummies Questions & Answers

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. File1.txt JOB1 10/09/2013 17:42:16 10/09/2013 17:43:46 SU 6202685/1... (4 Replies)
Discussion started by: mprithvi
4 Replies

5. UNIX for Dummies Questions & Answers

Date and time difference

Hi, I am trying to use the script as under : echo "Please input the string (APC) in the format (APC=x-yyy-z):" read a for i in m1 m2 m4 m5 m6 do cat /m12/$i/12* | grep -B 1 -A 1 inaccessible | gawk '/'$a'/{print $6,$7,$8,x};{x=$3" "$4}' | awk NF > temp cat /m122/$i/12* | grep -B 1 -A 1... (0 Replies)
Discussion started by: vanand420
0 Replies

6. Shell Programming and Scripting

Date and Time comparison using shell script

Hi, I'm having two fields in the file F1|F2 20111220|102000 F1 ->YYYYMMDD F2 ->HHMMSS Now, I need to compare this with current date & time and need to return the difference value in hours. Already, I checked with datecalc from the forum. So, need hints from Shell Gurus. Thanks (10 Replies)
Discussion started by: buzzusa
10 Replies

7. Shell Programming and Scripting

Date and time difference

I have start and finish date in the following format - Start Date: 5/21/2010 9:14:00 AM End Date : 5/24/2010 7:23:00 AM I need to get the time difference in the following format :mm or . Any help would be really appreciated. Thank you! (3 Replies)
Discussion started by: randev
3 Replies

8. Shell Programming and Scripting

Date and time difference

Hi, Below is the backup file name (includes date & time) : exp_trx_tables_18_Oct_2010_10_59_00.dmp Extracted date and time from the file name as below using the below "awk"command: date 18-Oct-2010 and time 10:59:00 echo "$fname" | awk -F"_" '{printf "Records will be restored as on... (1 Reply)
Discussion started by: milink
1 Replies

9. Shell Programming and Scripting

About date & time difference

Hello All, I was having a look on threads on the Forum about time calculation but didn't find exactly this issue. For instance, if we have these 2 dates, begin & end : 20100430235830 20100501000200 Is there anyway, awk, ksh, perl to calculate the difference in sec and get for... (6 Replies)
Discussion started by: rany1
6 Replies

10. UNIX for Dummies Questions & Answers

Date and time difference

Hi, I am starting a process at 9 pm today and want to exit the process by 5am, the next day. Every day at 9pm the shell script will be invoked by autosys. But how can i monitor in my script for 5 am and stop the process ? there may be a case where the script can start at 4.59 am and... (4 Replies)
Discussion started by: risshanth
4 Replies
Login or Register to Ask a Question