Date / Time difference in shell script

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.

Could this help you ?

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

I want it using simple BASH shell script.

@ ghosh_tanmoy what you have tried so far ? show us your script we shall help you.

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.

�ould this help you ?

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