我必须从数据中提取两个历元时间,然后我必须在bash中找到它们之间的区别



我有这个数据,其中我有两个epoch值,我必须提取该值,然后需要计算差异?

;;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

我试过下面的方法,但不知道如何计算差值。


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

上述命令的输出:-2021年12月1日星期三00:04:132021年12月1日星期三00:03:46


历元1=16388297252.658历元2=1638297253.078首先我必须从提到的字符串中提取历元值。然后为了计算时间间隔(或差异(,我试图将其转换为以下格式的

2021年12月1日星期三上午12:04:13 IST。

但即使在以上述格式转换了两个epoch之后,我也不知道该怎么计算差值。

我使用awk命令从字符串中提取字段。请原谅我解释不周

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

或者更容易阅读:

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

使用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

相关内容

最新更新