系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > server > anz > 详细页面

关于Grep的多次管道过滤的问题及解决

时间:2023-03-09来源:系统城装机大师作者:佚名

Grep的多次管道过滤问题

在日常的开发过程中,我们利用grep可以方便快捷的查找感兴趣的日志内容,极大地提升了开发和排错效率。但是有时候,我们也会遇到一些问题,比如。

  • crazy.log 是某个进程不断输出日志的文件
  • 我们使用tail -f crazy.log来检测日志的产生
  • 我们在前面的基础上利用管道增加一层过滤筛选感兴趣的内容。
1
2
3
4
5
6
7
8
9
10
11
tail -f crazy.log | grep Hello
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393

那么当我们再次增加一个过滤是,却没有内容(立即)产生了

1 /tmp tail -f crazy.log | grep Hello | grep Time

如何解决

1
2
3
4
5
6
tail -f crazy.log | grep --line-buffered Hello | grep Time
Hello,Time is 1566096393
Hello,Time is 1566096393
Hello,Time is 1566096393
Hello,Time is 1566096393
Hello,Time is 1566096393

如上,我们使用grep的选项--line-buffered即可。

line-buffered 是什么

--line-buffered
         Force output to be line buffered.  By default, output is line buffered when standard output is
         a terminal and block buffered otherwise.

上面的意思是

  • 强制输出结果使用行缓冲
  • 默认情况下,如果标准输入时终端,则使用line bufferred
  • 否则,使用块缓冲,(默认的大小为4096 bytes,因系统和配置而异)

所以,这也就解释了为什么双重grep过滤没有内容,因为没有达到块缓冲限制。

以上。

分享到:

相关信息

  • Windows下使用 Nginx 搭建 HTTP文件服务器 实现文件下载功能

    WHAT IS THE NGINX ? WHY WE USE NGINX? HOW TO USE NGINX ? Nginx有哪些应用? 动静分离 反向代理 反向代理是什么? 反向代理的作用 配置反向代理 负载均衡 负载均衡是什么? 配置负载均衡 正向代理...

    2023-03-09

  • centos7安装zabbix 5.0的详细过程

    安装前配置 安装postgresql 数据库安装配置 修改postgres密码 设置zabbix账户及数据库 安装zabbix 安装zabbix及前端 zabbix数据库初始化 配置zabbix server 前端php配置 启动zabbix服务器 前端设置 添加被监控机 被...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载