Home > Back-end >  I have to extract the two epoch time from the data and then i have to find difference between them i
I have to extract the two epoch time from the data and then i have to find difference between them i

Time:12-02

I have this data where i have two epoch value and i have to extract the value and then need to calculate there difference?

;;epoch1;;epoch 2 
617B96A302C71177;;1638297252.658;;1638297253.078;TTrans1;DEE61;66500;xxxxx;in;0x19;0x0;0;;scb:in;0x19;0x0;0;;sc:iund;0x0;ggp-ir:djkoe:ID 0: DSP: 1:trunk_02:ch_000

I tried below method but don't know how to calculate the difference.


#!/bin/bash
awk -F";;|;" -vOFS='\t' '{print strftime("%c", $3),  strftime("%c", $2)}' duration.txt(data is written in this file)

Output of above command:- Wed Dec 1 00:04:13 2021 Wed Dec 1 00:04:12 2021 Wed Dec 1 00:03:46 2021 Wed Dec 1 00:03:23 2021


epoch 1 =1638297252.658 epoch2 = 1638297253.078 first I have to extract the epochs value from the mentioned string. And then to calculate the time gap (or difference), I am trying to convert it into below format

Wednesday 01 December 2021 12:04:13 AM IST .

But even after converting both epoch in mentioned format i don't know what to do for calculating difference.

And I have used the awk command to extract the field from the string. please excuse my poor explanation

CodePudding user response:

awk -F ';' 'FNR!=1 {high = $3>$5?$3:$5; low = $3<$5?$3:$5; print high-low}' duration.txt

# gives
0.42

Or perhaps more readably:

awk -F ';' '
FNR!=1 {
    if ($3 > $5) {
        high=$3; low=$5
    } else {
        high=$5; low=$3
    }
    print high-low
}' duration.txt

CodePudding user response:

Using awk

$ awk -F";" '{$3=strftime("%A %d %B %Y %T %Z",$3); $5=strftime("%A %d %B %Y %T %Z",$5); hour=substr($3,17,2);min=substr($3,20,2);sec=substr($3,23,2); hour2=substr($5,17,2);min2=substr($5,20,2);sec2=substr($5,23,2)} NR > 1 {print "epoch 1: "$3"\nepoch 2: "$5"\nTime difference is "hour2 - hour" hours " min2-min" minutes " sec2 - sec" seconds"}' input_file
epoch 1: Tuesday 30 November 2021 18:34:12 GMT
epoch 2: Tuesday 30 November 2021 18:34:13 GMT
Time difference is 0 hours 0 minutes 1 seconds
$ cat script.awk
#!/usr/bin/env/ awk -f

BEGIN {
    FS=";"
} {
    $3=strftime("%A %d %B %Y %T %Z",$3)
    $5=strftime("%A %d %B %Y %T %Z",$5)
    hour=substr($3,26,2)
    min=substr($3,29,2)
    sec=substr($3,32,2)
    hour2=substr($5,26,2)
    min2=substr($5,29,2)
    sec2=substr($5,32,2)
} NR > 1 {
    print "epoch 1: "$3"\nepoch 2: "$5"\nTime difference is "hour2 - hour" hours " min2-min" minutes " sec2 - sec" seconds"
}
$ awk -f script.awk input_file
epoch 1: Tuesday 30 November 2021 18:34:12 GMT
epoch 2: Tuesday 30 November 2021 18:34:13 GMT
Time difference is 0 hours 0 minutes 1 seconds

CodePudding user response:

Getting difference in seconds:

awk -F ';' '{x = $5 - $3; print ((x < 0.0) ? -x : x)}' 

Explanations:

I have this data where i have two epoch value

Oh, then you already have the Seconds Since the Epoch for both.

Now you need to extract these two epoch timestamps from your SCSV.
With a header like ;;epoch1;;epoch 2, those fields would be $3 and $5 in awk -F ';'

Then, you just have to substract the two times and change the sign of the result when its negative.


Limitations: If your SCSV contains ; or \n characters in the field values then you will have to use an other tool than awk for parsing it.

  • Related