【问题标题】:tail -F log.log | grep ResponseTime | cut -d = -f 2尾-F log.log | grep 响应时间 |剪切-d = -f 2
【发布时间】:2011-03-14 06:26:22
【问题描述】:

我有一个名为 log.log 的实时日志文件,并希望在其中捕获一些匹配的模式和值:

示例: log.log 正在增长,我们正在搜索具有模式“ResponseTime = VALUE”的行,我们想要提取匹配的 VALUE:

我正在执行:

tail -F log.log | grep ResponseTime | cut -d = -f 2 | tr -d " "

我期待看到

VALUE1
VALUE2
.. etc

但它不起作用......我该怎么办?

谢谢你, 钠钼

============

谢谢,现在可以了。我在用: inotail -f log.log | stdbuf -oL grep 响应时间 |标准缓冲区 -oL 剪切 -d '=' -f 2 |标准缓冲区 -oL tr -d " "

【问题讨论】:

  • 如果你运行cat log.log | grep ResponseTime | cut -d = -f 2 | tr -d " "会发生什么?我怀疑管道中缓存了一些数据,并且在将更多数据写入log.log 文件(或 EOF)之前它不会被刷新。

标签: bash unix pipe


【解决方案1】:

【讨论】:

  • 谢谢,它有效。我正在使用:inotail -f log.log | stdbuf -oL grep 响应时间 |标准缓冲区 -oL 剪切 -d '=' -f 2 | stdbuf -oL tr -d " "
【解决方案2】:

尝试将grep 更改为stdbuf -oL grep。

详情请见BASHFAQ/009。

【讨论】:

    【解决方案3】:

    它不起作用的原因是某些命令不会在每个输出中刷新 STDOUT。因此后面的命令永远不会被传递任何东西。

    我会在tail 之后只使用一个命令,例如:

    tail -F t.log  | sed '/ResponseTime/!d;s/ResponseTime\s+=\s+(\d+)/\\1/'
    

    【讨论】:

      【解决方案4】:

      如果您想从输出中删除换行符,则可以执行以下任一操作:

      | cut -d = -f 2|sed -e :a -e '$!N;s/\n//;ta' 
      
      | cut -d = -f 2|tr -d '\n'
      
      | cut -d = -f 2|awk '{printf "%s",$0}'
      

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多