代码之家  ›  专栏  ›  技术社区  ›  les2

如何通过grep将输入管道传输到另一个实用程序?

  •  28
  • les2  · 技术社区  · 17 年前

    我使用'tail-f'跟踪日志文件的更新;接下来,我将输出管道传输到grep,只显示包含搜索词的行(在本例中为“org.springframework”);最后,我想做的是将grep的输出管道传输到第三个命令“cut”:

    tail -f logfile | grep org.springframework | cut -c 25-
    

    cut命令将为我删除每行的前25个字符 如果它能从grep获取输入!

    我在bash中使用cygwin。

    实际结果:当我添加第二个管道连接到“cut”命令时,结果是它挂起了,好像在等待输入(如果你想知道的话)。

    3 回复  |  直到 11 年前
        1
  •  10
  •   Dennis Williamson    17 年前

    --line-buffered 到你的命令行,例如。

    tail -f logfile | grep --line-buffered org.springframework | cut -c 25-
    

    编辑:

    您可能想尝试用您可以控制的东西替换它,例如sed:

    tail -f logfile | sed -u -n -e '/org\.springframework/ s/\(.\{0,25\}\).*$/\1/p'
    

    tail -f logfile | awk '/org\.springframework/ {print substr($0, 0, 25);fflush("")}'
    
        2
  •  29
  •   Hasturkun    17 年前

    tail -f logfile | while read line ; do echo "$line"| grep 'org.springframework'|cut -c 25- ; done
    
        3
  •  -1
  •   Adam Rosenfield    17 年前

    cut 我有(GNU coreutiles 6.10),你应该使用以下语法 cut -c 25-

    您还在两个示例中搜索不同的模式,以防相关。