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 is 3 milliseconds.

I want to print this in last of every line.

Like this

15:23:45,255 WARN <NTS>  RouteRequest : 518318 ### 15:23:45,258 CRIT <ISUP> RouteResponse : 518318 ||3
15:23:45,255 WARN <NTS>  RouteRequest : 518318 ### 15:23:45,258 CRIT <ISUP> RouteResponse : 518318
15:23:45,274 WARN <NTS>  RouteRequest : 518319 ### 15:23:45,278 CRIT <ISUP> RouteResponse : 518319
15:23:45,284 WARN <NTS>  RouteRequest : 518320 ### 15:23:45,286 CRIT <ISUP> RouteResponse : 518320
15:23:45,294 WARN <NTS>  RouteRequest : 518321 ### 15:23:45,296 CRIT <ISUP> RouteResponse : 518321

Thanks,
Raza

---------- Post updated at 05:00 PM ---------- Previous update was at 04:45 PM ----------

Hi All,

You can consider it like this also.

I have 3 fields in every line.

I want time difference in milli seconds as 4th column. It should be extracted from difference between column 2nd and 1st.

Original File.

15:23:45,212 15:23:45,215 518316
15:23:45,253 15:23:45,255 518317
15:23:45,255 15:23:45,258 518318
15:23:45,274 15:23:45,278 518319
15:23:45,284 15:23:45,286 518320
15:23:45,294 15:23:45,296 518321

Output

15:23:45,212 15:23:45,215 518316 # 3
15:23:45,253 15:23:45,255 518317 # 2
15:23:45,255 15:23:45,258 518318 # 3
15:23:45,274 15:23:45,278 518319 # 4
15:23:45,284 15:23:45,286 518320 # 2
15:23:45,294 15:23:45,296 518321 # 2

Present file has time diff at mili second level only, but in original file there can be difference at minute/second level also.

Regards,
Raza Ali

How about

awk 'function MS(TS) {n=split (TS,TMP, "[:,]"); return ((TMP[1]*60+TMP[2])*60+TMP[3])*1000+TMP[4]} {print $0 "||" MS($8) - MS($1)}' file
15:23:45,255 WARN <NTS>  RouteRequest : 518318 ### 15:23:45,258 CRIT <ISUP> RouteResponse : 518318||3
15:23:45,274 WARN <NTS>  RouteRequest : 518319 ### 15:23:45,278 CRIT <ISUP> RouteResponse : 518319||4
15:23:45,284 WARN <NTS>  RouteRequest : 518320 ### 15:23:45,286 CRIT <ISUP> RouteResponse : 518320||2
15:23:45,294 WARN <NTS>  RouteRequest : 518321 ### 15:23:45,296 CRIT <ISUP> RouteResponse : 518321||2

EDIT: This doesn't work crossing midnight, though.

1 Like

Hi Rudi,

Thank you for quick response.

Date in the log file is constant, So it won't be an issue.

Regards,
Raza Ali

$ cat time
15:23:45,255 WARN <NTS>  RouteRequest : 518318 ### 15:23:45,258 CRIT <ISUP> RouteResponse : 518318

Must set local decimal character / delimiter to be ',' and thousand delimiter to be '.'

$ awk -N 'BEGIN {FPAT="[0-9]+,[0-9]+ "} {print $0 " || "((($2-$1)*1000))}' time

otherwise

$ awk 'BEGIN {FPAT="[0-9]+,[0-9]+ "} {a=$1;b=$2;sub(/,/,".",a);sub(/,/,".",b); print $0 " || "(((b-a)*1000))}' time

Hi abdulbadii,
When I try your code with your sample input, your first suggestion produces the output:

awk: unknown option -N ignored

15:23:45,255 WARN <NTS>  RouteRequest : 518318 ### 15:23:45,258 CRIT <ISUP> RouteResponse : 518318 || -15000

and your second suggestion produces the output:

15:23:45,255 WARN <NTS>  RouteRequest : 518318 ### 15:23:45,258 CRIT <ISUP> RouteResponse : 518318 || -15000

which is exactly what I would expect when subtracting a string starting with 15: from the string WARN and multiplying the result by 1000.

If you had used $8 and $1 (instead of $2 and $1 ) with an awk that conforms to the standards, your results would have been 1000 times the difference between the hours portions of the two timestamps. What system are you using where the above code produces the output requested by the person who started this thread?

GNU awk replacement,

$ awk --version
GNU Awk 4.2.0, API: 2.0 (GNU MPFR 4.0.1, GNU MP 6.1.0)
Copyright (C) 1989, 1991-2017 Free Software Foundation.

Wow! I'm impressed. I would never have guessed that GNU awk ignored the standards so much that the code:

awk 'BEGIN{print ("WARN" - "15:23:45.255") * 1000}'

would produce the output:

3

(as requested by Razu Ali) instead of the output:

-15000

that would be produced by a version of awk that conforms to the standards.

Why do I find it hard to believe that your version of awk actually produces that output? :confused: