任何人都知道更好的方法来做到这一点,如果它更快?目前在将高行每秒推送到此脚本时很慢:
#!/bin/bash
declare -A clientarray
file=$1
timer=$2
e=$(date --date "now +$timer second" +%s)
while read line
do
if [ -n "${clientarray[$line]}" ]; then
let "clientarray[$line]=clientarray[$line]+1"
echo "$line: ${clientarray[$line]}"
elif [ -z "${clientarray[$line]}" ]; then
clientarray[$line]=1
echo "$line: ${clientarray[$line]}"
fi
if [ $(date +%s) -gt $e ]; then
e=$(date --date "now +$timer second" +%s)
fi
done < <(tail -F $file | gawk -F"]" '/]/ {print $1}')
这是行的示例:
someline]
someline2]
somethingidontwant
someline3]
somethingelseidontwant
someline4]
并调用脚本:
bash script.sh somelogfile.log 1
如果我在最后注释掉 if 逻辑,它会非常快,但速度会下降 2/3。用 pv 测试它:
(这与 if 逻辑有关):
ubuntu@myhost:~/graphs$ tail -F somelogfile.log | pv -N RAW -lc >/dev/null |
> bash script.sh somelogfile.log 1 | pv -N SCP -lc >/dev/null
RAW: 2.18k 0:00:16 [ 493/s ] [ <=> ]
SCP: 593 0:00:16 [ 150/s ] [ <=> ]
(这是没有)
ubuntu@myhost:~/graphs$ tail -F somelogfile.log | pv -N RAW -lc >/dev/null |
> bash script.sh somelogfile.log 1 | pv -N SCP -lc >/dev/null
RAW: 7.69k 0:00:15 [512/s] [ <=> ]
SCP: 7.6k 0:00:15 [503/s] [ <=> ]
如果我在脚本或测试方面遗漏了什么,请告诉我,尤其是任何“DOH!”。我想在这一点上我会喜欢一个=)