log.txt
将如下所示,这些是具有自己时间戳(detection_time)的 ID 数据,将在此 log.txt 文件中不断更新。ID 数据将是不可预知的数字。它可能是从 0000-9999 并且相同的 ID 可能会再次出现在 log.txt 中。
log.txt
我的目标是使用 shell 脚本过滤在第一次出现后 15 秒内再次出现的 ID 。谁能帮我这个?
ID = 4231
detection_time = 1595556730
ID = 3661
detection_time = 1595556731
ID = 2654
detection_time = 1595556732
ID = 3661
detection_time = 1595556733
更清楚地说,从log.txt
上面看,ID 3661 首先出现在时间 1595556731,然后在 1595556733 再次出现,这距离第一次出现仅 2 秒。所以它符合我想要在 15 秒内再次出现的 ID 的条件。我希望这个 ID 3661 被我的 shell 脚本过滤
运行 shell 脚本后的输出将是
ID = 3661
我的问题是我不知道如何在 shell 脚本中开发编程算法。
这是我尝试使用ID_new
和 ID_previous
变量但ID_previous=$(ID_new) detection_previous=$(detection_new)
不工作的方法
input="/tmp/log.txt"
ID_previous=""
detection_previous=""
while IFS= read -r line
do
ID_new=$(echo "$line" | grep "ID =" | awk -F " " '{print $3}')
echo $ID_new
detection_new=$(echo "$line" | grep "detection_time =" | awk -F " " '{print $3}')
echo $detection_new
ID_previous=$(ID_new)
detection_previous=$(detection_new)
done < "$input"
EDIT
log.txt
实际上数据在一个集合中,包含 ID、detection_time、Age 和 Height。很抱歉一开始没有提到这个
ID = 4231
detection_time = 1595556730
Age = 25
Height = 182
ID = 3661
detection_time = 1595556731
Age = 24
Height = 182
ID = 2654
detection_time = 1595556732
Age = 22
Height = 184
ID = 3661
detection_time = 1595556733
Age = 27
Height = 175
ID = 3852
detection_time = 1595556734
Age = 26
Height = 156
ID = 4231
detection_time = 1595556735
Age = 24
Height = 184
我已经尝试过 awk 解决方案。结果是
4231
3661
2654
3852
4231
log.txt 中的所有 ID 正确的输出应该是4231
3661
由此,我认为 Age 和 Height 数据可能会影响 Awk 解决方案,因为它插入在 ID 和 detection_time 的焦点数据之间。