Update time stamp and replace values

Can anyone please assist?

I need to grab each line between the lines "HEADER" and "TRAILER"

Each line contains two timestamps(Timestamp1) and (Timestamp2). I need to create two variables TIME_SUBSTRCT and TIME_ADD and

then recalculate the two timestamps using this logic:
START_TIME=Timestamp1 - ${TIME_SUBSTRACT}
END_TIME=Timestamp2 + ${TIME_ADD}
The final output should be each line with its new adjusted timestamps in place of the original timestamps.

eg. If timestamp1=09:17:00 and TIME_SUBSTRACT=10 , the START_TIME to be 09:07:00

If timestamp2=10:15:00 and TIME_ADD=15, the END_TIME to be 10:30:00

Input File:

HEADER
SERVER1 02/10/2016 10:13:00.000 02/10/2016 10:18:59.011 1123
SERVER2 02/10/2016 07:05:00.000 02/10/2016 08:10:59.011 1234
SERVER3 02/10/2016 06:32:00.000 02/10/2016 07:37:59.012 1567
SERVER4 02/11/2016 03:14:00.000 02/11/2016 08:19:59.134 1678
SERVER5 02/10/2016 11:48:00.000 02/10/2016 06:53:59.444 1790
TRAILER

Expected output:

SERVER1 02/10/2016 10:03:00.000 02/10/2016 10:33:59.011 1123
SERVER2 02/10/2016 06:55:00.000 02/10/2016 08:25:59.011 1234
SERVER3 02/10/2016 06:22:00.000 02/10/2016 07:52:59.012 1567
SERVER4 02/11/2016 03:04:00.000 02/11/2016 08:34:59.134 1678
SERVER5 02/10/2016 11:38:00.000 02/10/2016 07:08:59.444 1790
My Code:
######################
TIME_SUBSTRACT=10
TIME_ADD=15
InputFile=Inputfile.txt
ExtractFile=Extractfile.txt

awk '/HEADER/ {flag=1;next} /TRAILER/{flag=0} flag {print}' ${InputFile}  >${ExtractFile}

ReplaceValue()
{
awk  -v var="${START_TIME}" -v var1="${END_TIME}"  '{$3=var;$5=var1}1' ${ExtractFile} >outputfile.txt
}

cat ${ExtractFile} | while read line
do
TIMESTAMP1=`echo $line | awk '{print substr($3,1,8)}'`
TIMESTAMP2=`echo $line | awk '{print substr($5,1,8)}'`
START_TIME=`awk -v var="${TIME_SUBSTRACT}" -v var1="${TIMESTAMP1}" 'BEGIN{print strftime("%H:%M:%S",var1-(var*60))}'`
END_TIME=`awk -v var="${TIME_ADD}" -v var1="${TIMESTAMP2}" 'BEGIN{print strftime("%H:%M:%S",var1+(var*60))}'`
ReplaceValue
done

Few issues 1) Not getting correct start time and end time. 2) while replacing the older timestamp with new timestamp last part of columns 3 and 5 are missing(Ex:000,011). Tried using substr and its not working.

Thanks for your help in advance.

---------- Post updated 03-15-16 at 09:45 AM ---------- Previous update was 03-14-16 at 02:09 PM ----------

I'm getting correct start time and end time. I should be able to fix the code.

Thanks

I've got this for you, but it doesn't yield the correct seconds nor decimals:

awk  -vTS=-10 -vTA=+15 '
function GTM(T,D)       {cmd = "date +\"%d/%m/%Y %H:%M:%S.000\" -d\"" T D "min\""
                         cmd | getline X
                         close (cmd)
                         return X
                        }
NF == 1                 {print
                         next
                        }
                        {gsub (/:|:..\..*$/, "", $3)
                         gsub (/:|:..\..*$/, "", $5)
                         print $1, GTM($2 " " $3, TS), GTM($4 " " $5, TA), $6
                        }
'  file
1 Like

Rudic, Thanks a lot for your help !

I tried epoch time logic and my code is working fine.

Hi RudiC,
after spending so much of time i am unable to understand the function used in code.

function GTM(T,D)       {cmd = "date +\"%d/%m/%Y %H:%M:%S.000\" -d\"" T D "min\""
                         cmd | getline X
                         close (cmd)
                         return X
                        }

till here i can discern that you got date value in T as 02/10/2016 1013 and -10 in D
.Could you please explain how cmd is being calculated specially -d and min in -d\"" T D "min\"" . And here | is operating as OR or passing value from cmd to getline. Why we need to close cmd , because close is used to close file but cmd holds some value here.

Thanks,

Yes, T is the time, and D is the delta. In awk, the | pipes the cmd command's result to getline . And, whatever the cmd is, it can and should be closed whenever the max open files is exceeded.

Why don't you print the cmd in the function just to see what's in there?

If you have GNU awk you could use the mktime() and strftime() like this:

gawk -F "[ :/.]" -v TSUB=10 -v TADD=15 '
 /TRAILER/ {flg=0}
 flg {
   T1=mktime($4 " " $2 " " $3 " " $5 " " $6 " " $7)
   T2=mktime($11 " " $9 " " $10 " " $12 " " $13 " " $14)
   TS1=strftime("%m/%d/%Y %T",T1 - TSUB*60)"."$8
   TS2=strftime("%m/%d/%Y %T",T2 + TADD*60)"."$15
   print $1 , TS1 , TS2 , $16
 }
 /HEADER/ {flg=1}' infile > outfile