【问题标题】:Grep for string if contained in several consecutive lines如果包含在多个连续行中,则为字符串 Grep
【发布时间】:2018-04-19 14:20:14
【问题描述】:

我有一个日志文件,我想在其中查找包含特定字符串的行,如果它至少有五个连续的行包含它。

例如我的日志文件如下所示:

Sent RTP packet to      10.124.193.26:12486 (type 09, seq 030081, ts 2726949472, len 000160)
Got  RTP packet from    10.124.193.26:12486 (type 09, seq 052435, ts 174852800, len 000160)
Sent RTP packet to      10.124.193.26:12486 (type 09, seq 030082, ts 2726949632, len 000160)
Got  RTP packet from    10.124.193.26:12486 (type 09, seq 052436, ts 174852960, len 000160)
Sent RTP packet to      10.124.193.26:12486 (type 09, seq 030083, ts 2726949792, len 000160)
Got  RTP packet from    10.124.193.26:12486 (type 09, seq 052437, ts 174853120, len 000160)
Sent RTP packet to      10.124.193.26:12486 (type 09, seq 030084, ts 2726949952, len 000160)
Got  RTP packet from    10.124.193.26:12486 (type 09, seq 052438, ts 174853280, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055416, ts 87947536, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055417, ts 87947696, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055418, ts 87947856, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055419, ts 87948016, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055420, ts 87948176, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055421, ts 87948336, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055422, ts 87948496, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055423, ts 87948656, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055424, ts 87948816, len 000160)
Sent RTP packet to      10.124.193.90:11836 (type 09, seq 055425, ts 87948976, len 000160)

第一行以“Sent”或“Got”交替开头。 这些都很好,我对它们不感兴趣。 然后是仅以“已发送”开头的连续行。 那些我想找到的。 如何使用 grep 或其他命令找到它们?

【问题讨论】:

    标签: grep


    【解决方案1】:

    您可以使用以下命令:

    $ cat <filename> | awk '{ if( pre == "Sent" && $1 == "Sent" ) {print $0} ; {pre=$1} }'
    

    解释:

    记住最后一行的第一个单词。如果是“Sent”,并且当前行的第一个单词也是“Sent”,则打印当前行。

    【讨论】:

    • 谢谢。当连续两行以“已发送”开头时打印。仅当连续 5 行以“已发送”开头时,我才需要打印。可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2014-03-22
    • 2013-09-24
    • 2011-01-22
    相关资源
    最近更新 更多