【发布时间】:2018-10-08 02:37:21
【问题描述】:
我正在监视一个主动写入文件:
我目前的解决方案是:
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | grep -q -e "enterpriseID:"
if [ $? = 0 ]
then
((ws_trans++))
fi
echo $LINE | grep -q -e "sc_ID:"
if [ $? = 0 ]
then
((sc_trans++))
fi
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
但是,当尝试使用 AWK 执行此操作时,我没有得到输出 - $ws_trans 和 $sc_trans 仍然是 0
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | awk '/enterpriseID:/ {++ws_trans} END {print | ws_trans}'
echo $LINE | awk '/sc_ID:/ {++sc_trans} END {print | sc_trans}'
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
尝试这样做以减少负载。我知道 AWK 不处理 bash 变量,它可能会让人很困惑,但我发现的唯一参考是 AWK 的非尾部应用程序。
如何将 AWK 变量分配给 bash ws_trans 和 sc_trans?有更好的解决方案吗? (还有其他搜索词受到监控。)
【问题讨论】: