log.txt
as below, the data is in a set contain ID, detection_time, Age and Height.
My first part is to print the ID that appears again in the log.txt
within 15 sec from its first appearance by using shell script which i've done it already. The second part is to print "detection_time", "Age" "Height" of the ID that match to my condition will also be printed with the ID too.
The second part is where im stucked because i don't know how to develop the programming algorithm.
ID = 4231
detection_time = 1595556730
Age = 25
Height = 182
ID = 3661
detection_time = 1595556737
Age = 24
Height = 182
ID = 2654
detection_time = 1595556740
Age = 22
Height = 184
ID = 3661
detection_time = 1595556746
Age = 27
Height = 175
ID = 4231
detection_time = 1595556752
Age = 25
For example, log from above, the ID 3661 first appear at time 1595556737 and then appear again at 1595556746 which is just 9 sec after the first appearance. So it is matched to my condition which is want the ID that appear again within 15sec. After run a shell script, my desired output will be ID3661
with its latest detection_time
Age
and Height
data of this ID 3661 which are
the matched ID is 3661
detection_time = 1595556746
Age = 27
Height = 175
Here is my code. I use associative array arr
where id as key and detection_time as value. After run this script where above logfile as an input. The output will be The matched ID is 3661
without the ID detection_time, Age and Height which is where im stucked. Can anyone help me with this? Thank you.
#!/bin/bash
input="/tmp/log.txt"
declare -A arr
while read -r line
do
if [[ $line =~ ID ]] ; then
id=$(echo $line | awk -F " " '{print $3}')
elif [[ $line =~ detection_time ]] ; then
dtime=$(echo $line | awk -F " " '{print $3}')
if [[ arr["$id"] -ge $((dtime - 15)) ]]; then
echo 'The matched ID is' "$id"
fi
arr["$id"]=$dtime
fi
done < "$input"