加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash-tail -f进入grep切割不能正常工作

发布时间:2020-12-15 18:41:50 所属栏目:安全 来源:网络整理
导读:我正在尝试构建一个 shell脚本来监视一些日志文件. 我正在使用如下命令: tail -f /var/somelog | grep --line-buffered " some test and p l a c e h o l d e r" | cut -f 3,4,14 -d " " 日志文件如下: some test and p l a c e h o l d e r 3some test an
我正在尝试构建一个 shell脚本来监视一些日志文件.
我正在使用如下命令:
tail -f /var/somelog | grep --line-buffered " some test and p l a c e h o l d e r" | cut -f 3,4,14 -d " "

日志文件如下:

some test and p l a c e h o l d e r 3
some test and p l a c e h o l d e r 4
some test and p l a c e h o l d e r 5
some test and p l a c e h o l d e r 6

等等..
我的问题是命令的输出不显示最后一行

some test and p l a c e h o l d e r 6

直到行

some test and p l a c e h o l d e r 7

被添加到日志中.

我希望我明白我的问题.任何人都可以帮我解决这个问题吗?
谢谢 :)

问题几乎肯定与grep和cut如何缓冲输出有关.这是一个黑客,应该让你解决问题,虽然我相信有更好的方法来做到这一点:
tail -f /var/somelog | while read line; do echo "$line" | grep "some test and p l a c e h o l d e r" | cut -f 3,14 -d " "; done

(不要忘记;完成命令结束)

或者,因为gawk不缓冲它的输出,你可以使用它代替剪切,以避免繁琐的循环:

tail -f log | grep --line-buffered "some test and p l a c e h o l d e r" | gawk '{print $3,$4,$14}'

查看http://www.pixelbeat.org/programming/stdio_buffering/有关缓冲问题的更多信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读