如何在Linux中以不同的行表示不同的单词

时间:2019-03-15 19:19:09

标签: linux shell unix grep

我正在尝试使用tail -f命令监视日志,并且每当我在同一日志的不同行中遇到两个不同的单词时,都需要捕获它们并发送电子邮件通知:

例如:cat example.txt

<event> 12345 </event>
<Description> Exception on file transfer for user ABC </Description>

我需要监视用户'ABC'具有'Exception'的事件'12345'。

当我这样做tail -F example.txt | egrep "12345|Exception|ABC"时 如果看到Grep关键字中的任何一个,则打印命令。相反,仅当遇到grep中的所有关键字时才需要打印。

1 个答案:

答案 0 :(得分:1)

尝试

tail -f example.txt | egrep --line-buffered  "Exception.*ABC" -B 1 | egrep -v Description
  • “ Exception。* ABC”搜索具有Exception AND ABC的行
  • “-B 1”要有前一行(事件)
  • “-line-buffered”将打开grep缓冲模式(否则第二个egrep将不再处理)
  • “ | egrep -v Description”最终删除了Description行(因为您只想要事件)

您最终将拥有

    <event> 12345 </event>

播放每个参数以查看差异

致谢