Grep(排除)具有正则表达式匹配下一行的行

时间:2017-04-27 17:14:11

标签: regex grep

我有一个日志文件,我试图grep -v所有不必要的信息,所以我只能看到有用的信息。如果后面的下一行也是日期,我似乎无法弄清楚如何排除日期。

到目前为止我所拥有的:

Fri Apr  7 01:11:01 PDT 2017
Upgrading certbot-auto 0.12.0 to 0.13.0...
Replacing certbot-auto...
Installation succeeded.
Sat Apr  8 01:11:01 PDT 2017
Sun Apr  9 01:11:01 PDT 2017
Mon Apr 10 01:11:01 PDT 2017
Tue Apr 11 01:11:01 PDT 2017
Wed Apr 12 01:11:01 PDT 2017
Thu Apr 13 01:11:01 PDT 2017
Fri Apr 14 01:11:01 PDT 2017
Sat Apr 15 01:11:01 PDT 2017
Sun Apr 16 01:11:01 PDT 2017
Mon Apr 17 01:11:01 PDT 2017
Tue Apr 18 01:11:01 PDT 2017
Wed Apr 19 01:11:01 PDT 2017
Thu Apr 20 01:11:01 PDT 2017
Fri Apr 21 01:11:01 PDT 2017
WARNING: unable to check for updates.
Sat Apr 22 01:11:01 PDT 2017
Sun Apr 23 01:11:01 PDT 2017
Mon Apr 24 01:11:01 PDT 2017
Tue Apr 25 01:11:01 PDT 2017

我想要的是什么:

Fri Apr  7 01:11:01 PDT 2017
Upgrading certbot-auto 0.12.0 to 0.13.0...
Replacing certbot-auto...
Installation succeeded.
Fri Apr 21 01:11:01 PDT 2017
WARNING: unable to check for updates.

3 个答案:

答案 0 :(得分:0)

<强>解决!

使用它:

grep -v '^.*\(PDT\|PST\)\s*[0-9]\{4\}' -B 1 | grep -v '^--$'

这是最后一个命令:

cat certbot.log |
grep -v '^-*$' |
grep -v '^Processing ' |
grep -v '(skipped)' |
grep -v 'No renewals were' |
grep -v 'not due for renewal yet' |
grep -v 'No hooks' |
grep -v 'DeprecationWarning' |
grep -v 'not yet due for' |
grep -v '^Saving debug' |
grep -v 'Installing Python packages' |
grep -v 'Creating virtual' |
grep -v '^.*\(PDT\|PST\)\s[0-9]\{4\}' -B 1 |
grep -v '^--$' |
sed '/\(PDT\|PST\)/i\\n' |
sed 's/.*\(PDT\|PST\).*/--- & --- /'

以下是最终结果:

--- Fri Mar  3 01:11:01 PST 2017 ---
Upgrading certbot-auto 0.11.1 to 0.12.0...
Replacing certbot-auto...
Installation succeeded.


--- Thu Mar 23 01:11:01 PDT 2017 ---
WARNING: unable to check for updates.


--- Wed Mar 29 01:11:01 PDT 2017 ---
Cert is due for renewal, auto-renewing...
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for {mydomain}.com
Waiting for verification...
Cleaning up challenges
Generating key (2048 bits): /etc/letsencrypt/keys/0001_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0001_csr-certbot.pem
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/{mydomain}.com/fullchain.pem
Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/{mydomain}.com/fullchain.pem (success)


--- Thu Apr  6 01:11:01 PDT 2017 ---
WARNING: unable to check for updates.


--- Fri Apr  7 01:11:01 PDT 2017 ---
Upgrading certbot-auto 0.12.0 to 0.13.0...
Replacing certbot-auto...
Installation succeeded.


--- Fri Apr 21 01:11:01 PDT 2017 ---
WARNING: unable to check for updates.

答案 1 :(得分:0)

我想出了这个。看看这个是否有帮助。

正则表达式: ([A-Za-z]{3}\s[A-Za-z]{3}\s*\d{1,2}\s(?:\d{2}:){2}\d{2}\s[A-Z]{3}\s\d{4}\n?){2,}

Regex101 Demo

答案 2 :(得分:0)

如果您perl已安装,请尝试在shell上运行:

perl -0777 -ne 'while(m/^(.*?succeeded\..*?\d{4}.)|WARNING:.*?\./simg){print "$&";}'  your_file

<强>输出:

Fri Apr  7 01:11:01 PDT 2017
Upgrading certbot-auto 0.12.0 to 0.13.0...
Replacing certbot-auto...
Installation succeeded.
WARNING: unable to check for updates.

Regex Demo