本文翻译自:How to 'grep' a continuous stream?

Is that possible to use grep on a continuous stream? 可以在连续流上使用grep吗?

What I mean is sort of a tail -f <file> command, but with grep on the output in order to keep only the lines that interest me. 我的意思是有点tail -f <file>命令,但是在输出中使用grep以便仅保留我感兴趣的行。

I've tried tail -f <file> | grep pattern 我已经试过tail -f <file> | grep pattern tail -f <file> | grep pattern but it seems that grep can only be executed once tail finishes, that is to say never. tail -f <file> | grep pattern但似乎只能在tail完成后才执行grep ,也就是说永远不会执行。


#1楼

参考:https://stackoom.com/question/U37F/如何-grep-连续流


#2楼

Use awk(another great bash utility) instead of grep where you dont have the line buffered option! 如果没有行缓冲选项,请使用awk(另一个出色的bash实用程序)代替grep! It will continuously stream your data from tail. 它将持续从尾部流式传输您的数据。

this is how you use grep 这就是你使用grep的方式

tail -f <file> | grep pattern

This is how you would use awk 这就是你使用awk的方式

tail -f <file> | awk '/pattern/{print $0}'

#3楼

In most cases, you can tail -f /var/log/some.log |grep foo and it will work just fine. 在大多数情况下,您可以在tail -f /var/log/some.log |grep foo ,它将正常工作。

If you need to use multiple greps on a running log file and you find that you get no output, you may need to stick the --line-buffered switch into your middle grep(s), like so: 如果您需要在运行中的日志文件上使用多次抓取,但发现没有任何输出,则可能需要将--line-buffered开关粘贴到中间的 grep中,如下所示:

tail -f /var/log/some.log | grep --line-buffered foo | grep bar

#4楼

you may consider this answer as enhancement .. usually I am using 您可能会将此答案视为增强..通常我正在使用

tail -F <fileName> | grep --line-buffered  <pattern> -A 3 -B 5

-F is better in case of file rotate (-f will not work properly if file rotated) -F在文件旋转的情况下更好(如果文件旋转,-f将无法正常工作)

-A and -B is useful to get lines just before and after the pattern occurrence .. these blocks will appeared between dashed line separators -A和-B对于在模式发生之前和之后获取行很有用..这些块将出现在虚线分隔符之间

But For me I prefer doing the following 但对我来说,我更喜欢执行以下操作

tail -F <file> | less

this is very useful if you want to search inside streamed logs. 如果要在流式日志中进行搜索,这将非常有用。 I mean go back and forward and look deeply 我的意思是来回向前看


#5楼

sed would be a better choice ( stream editor) sed是更好的选择( 编辑器)

tail -n0 -f <file> | sed -n '/search string/p'

and then if you wanted the tail command to exit once you found a particular string: 然后,如果您希望在找到特定字符串后退出tail命令:

tail --pid=$(($BASHPID+1)) -n0 -f <file> | sed -n '/search string/{p; q}'

Obviously a bashism: $BASHPID will be the process id of the tail command. 显然是一种批评:$ BASHPID将是tail命令的进程ID。 The sed command is next after tail in the pipe, so the sed process id will be $BASHPID+1. sed命令位于管道尾部之后,因此sed进程ID为$ BASHPID + 1。


#6楼

If you want to find matches in the entire file (not just the tail), and you want it to sit and wait for any new matches, this works nicely: 如果您想在整个文件中查找匹配项(而不仅仅是尾部),并且希望它坐下来等待任何新的匹配项,则效果很好:

tail -c +0 -f <file> | grep --line-buffered <pattern>

The -c +0 flag says that the output should start 0 bytes ( -c ) from the beginning ( + ) of the file. -c +0标志表示输出应从文件的开头( + )开始0个字节( -c )。

如何“ grep”连续流?相关推荐

  1. Linux 运维常用命令 find、awk、sed、grep、vi、ps、lsof、rpm

    先用一个脚本,模拟创建 14 个测试文件: #!/bin/bash for ((i=1;i<=10;i++)); do if [ $i -lt 3 ] thentouch /home/mysql ...

  2. Linux shell 学习笔记(2)— 监测程序、磁盘空间和处理文件(ps -ef、top、kill、df、du 、grep、tar)

    1. 监测程序 1.1 检查进程 默认情况下,ps 命令只会显示运行在当前控制台下的属于当前用户的进程. $ ps PID TTY TIME CMD 3081 pts/0 00:00:00 bash ...

  3. grep 的特殊使用

    反向引用 捕获会返回一个捕获组,这个分组是保存在内存中的,不仅可以在正则表达式外部通过程序进行引用,也可以在正则表达式内部进行引用,这种引用方式就是反向引用. 根据捕获组的命名规则,反向引用可分为: ...

  4. logcat 结合 grep 过滤 log 日志信息

    打开终端: 1 adb devices 查看是否连接 2 连接之后使用adb shell 3  logcat | grep "关键字" 这样就能过滤日志了 当然如果想忽略大小写可以 ...

  5. grep 使用 nsr 实现查找

    有时候我们在查找某个方法或者布局之后在那些地方使用 ,可以使用开发工具自带的全局查找,有时候很慢, 我们可以试下 grep 的nsr 命令查找 个人感觉还是很快的 grep -nsr "TA ...

  6. Windows 系统下使用grep 命令

    这2年一直在使用Ubuntu 系统开发, 使用grep命令 很方便,今天在windows 终端里面使用 提示不识别,为了下同事说在git 里面使用可以,试了下再git 里面是可以使用的 首先电脑需要先 ...

  7. Linux中锚定符号的作用,Linux基础(9)文本处理三剑客之grep

    grep的作用:根据指定的模式搜索文本,将匹配到的文本行显示出来 语法格式:grep [options] "PATTERN" file PATTERN为正则表达式,grep一般是与 ...

  8. ATS 5.3.0中利用grep得到纯净的配置文件

    ATS 5.3.0中的配置文件通常会标有详细的注释,但是我们有时候需要纯净的配置文件,特别是在线上部署的配置文件,不需要这么详细的注释.下面是处理过程. cat records.config | gr ...

  9. grep 在HP-UX下的递归查找

    grep 在HP-UX下的递归查找 Linux: man grep 可以看到 -r 选项  -R, -r, --recursive               Read all files under ...

最新文章

  1. leetcode算法题--复制带随机指针的链表★
  2. 在联盛德W600 SDK上添加C文件并实现GCC编译
  3. smart filter无法从smart business应用获得值的问题分析
  4. html5游戏指尖跟随,图片跟随手指滑动
  5. linux下查看一个进程的启动时间和运行时间
  6. CentOS 系统盘迁移
  7. 宏定义 是否retina高清屏,判断当前设备
  8. mkpasswd命令
  9. Facebook首次披露自研AI芯片细节:为视频而生,LeCun挂帅
  10. OCS2007标准版服务器部署
  11. Android内存泄漏(转)
  12. SHELL中如何对一个变量进行算术操作(加减)
  13. __declspec(dllimport)和__declspec(dllexport)的区别,以及有关c/c++调用约定
  14. 2022江苏省大学生电子设计竞赛(TI杯)赛后回顾(江苏省二等奖)
  15. Xmind思维导图模板分享
  16. 简介计算机桌面功能,desktopcal电脑桌面日历功能介绍
  17. DigiCert SSL证书支持中文域名申请吗?
  18. 自然语言处理 第二期
  19. VIVADO安装问题
  20. 豆瓣评分小程序Part-3

热门文章

  1. HttpComponents组件探究 - HttpClient篇
  2. AD排错最佳实践—— 技巧与捷径-笔记
  3. Java中利用package机制实现条件编译
  4. 20171016课程随笔
  5. 对Table_locks_immediate值的理解
  6. 设计模式(七): 通过转接头来观察适配器模式(Adapter Pattern)
  7. Activity的四种启动模式区别
  8. install and use zookeeper C client API
  9. struts2+spring 项目中配置tiles的解决方案
  10. weblogic时间问题