检查并从编号列表重新使用旧号码 [英] check and reuse old numbers from a numbered list
问题描述
我有产生过像这样时间编号输出RPI在Debian中运行,每一行的第一个字符串的脚本用来组匹配的项目一起:
I have a script running in Debian on a RPi which generates over time numbered outputs like so, with the first string of each line being used to group matching items together:
/1 AtherosC_92:f1:a7 BTHub4-NJ8S -82
/1 AtherosC_92:f1:a7 BTHub4-NJ8S -81
/2 95:8c:ed:6d:65:f5 Home245 -84
/1 AtherosC_92:f1:a7 BTHub4-NJ8S -78
/3 3d:cc:54:d1:4f:f6 BTWifi2 -82
/4 Apple_e5:e8:2d SKYBD80F -71
/4 Apple_e5:e8:2d SKYBD80F -71
/4 Apple_e5:e8:2d SKYBD80F -72
通过使用以下脚本进行分组:
With the grouping done with the following script:
declare -Ai seen=()
while read && read -r first _ <<< "$REPLY" ; do
[[ $first ]] && printf '/%d %s\n' "${seen["$first"]=$((${#seen[@]}+1))}" "$REPLY"
done
在每个列表的开头的号码被用于它们在目标程序进行排序。我需要一种方法产生一个较大的数字,看是否有更低的数量已经拿出了最近(最后30秒左右)之前检查并重新使用这个数字,如果它没有。因此,举例来说,如果没有被主命令> 30秒过/ 1路输出,当会产生第三个唯一的字符串出现以下情况:
The number at the start of each list is being used to sort them in the destination program. I need a method to check before generating a higher number to see if a lower number has come up recently (in the last 30 seconds or so) and reuse that number if it has not. So for example, if /1 had not been output by the primary command in >30 sec, the following would occur when a third unique string is generated:
/1 AtherosC_92:f1:a7 BTHub4-NJ8S -82
/1 AtherosC_92:f1:a7 BTHub4-NJ8S -81
/2 95:8c:ed:6d:65:f5 Home245 -84
/1 3d:cc:54:d1:4f:f6 BTWifi2 -82
是否有这如何能实现什么建议?无论是在bash或别的什么吗?
Are there any suggestions as to how this could be achieved? Either in bash or anything else?
推荐答案
这是<一个后续href=\"http://stackoverflow.com/questions/29049326/generate-unique-tags-to-repeating-results-of-a-command/29049572?noredirect=1#comment46791504_29049572\">Generate独特的标签重复命令结果
下面是一个可能性:我们将介绍两个阵列,标记
和 tags_time
。它们的键是使用的变量; 标记
的字段在输出相应的字符串(关联数组的键看到
),和田野的 tags_time
是在标签最后一次使用。
Here's a possibility: we'll introduce two more arrays, tags
and tags_time
. Their keys are the tags used; the fields of tags
are the corresponding string in your output (the keys of the associative array seen
), and the fields of tags_time
are the timestamps when the tag was last used.
然后,我们引入一个函数 get_tag
的解析整数从 1
起来,如果一个整数是免费的(即对应于无键或已使用更多的延迟
秒前),从我们的数据删除,并返回它使用新的索引。
We then introduce a function get_tag
the parses the integers from 1
up, and if an integer is free (i.e., corresponds to no keys or has been used more that delay
seconds ago), delete it from our data and return it as the new index to use.
#!/bin/bash
declare -A seen
tags=()
tags_time=()
delay=30
get_tag() {
local i
for ((i=1;;++i)); do
if [[ -z ${tags[i]} ]] || ((tags_time[i] < SECONDS-delay)); then
break
fi
done
if [[ ${tags[i]} ]]; then
unset 'seen[${tags[$i]}]' "tags[i]" "tags_time[i]"
fi
get_tag_ret=$i
}
while read; do
read -r first _ <<< "$REPLY"
[[ $first ]] || continue
if [[ -z ${seen["$first"]} ]]; then
get_tag
seen["$first"]=$get_tag_ret
tags[get_tag_ret]=$first
tags_time[get_tag_ret]=$SECONDS
fi
printf '\\%d %s\n' "${seen["$first"]}" "$REPLY"
done
我没有测试它...所以裸跟我来!
I haven't tested it… so bare with me!
这篇关于检查并从编号列表重新使用旧号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!