複数行grep

pcregrep というperl的なgrepを行うコマンドがある。

$ openssl s_client -no_ssl3 -connect example.com:443 < /dev/null 2>&1
CONNECTED(00000003)
depth=1 /C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/CN=example.com
   i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
 1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
   i:/O=Digital Signature Trust Co./CN=DST Root CA X3
---
Server certificate
-----BEGIN CERTIFICATE-----
...

$ openssl s_client -no_ssl3 -connect example.com:443 < /dev/null 2>&1 | pcregrep --multiline "Certificate chain[\s\S]*?---"
Certificate chain
 0 s:/CN=example.com
   i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
 1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
   i:/O=Digital Signature Trust Co./CN=DST Root CA X3
---

参考

regex – How to find patterns across multiple lines using grep? – Stack Overflow