title: How to get the text between the last occurrence of a pair of strings
body: <p>I need to extract text between the last occurrence of a word called &quot;-----BEGIN CERTIFICATE-----&quot; and the last occurrence of a word called &quot;-----END CERTIFICATE-----&quot;.</p>
  <p><strong>Input :</strong></p>
  <pre><code>some other data above this line
  -----BEGIN CERTIFICATE-----
  a
  b
  c
  -----END CERTIFICATE-----
  -----BEGIN CERTIFICATE-----
  d
  e
  f
  -----END CERTIFICATE-----
  -----BEGIN CERTIFICATE-----
  g
  h
  i
  -----END CERTIFICATE-----
  -----BEGIN CERTIFICATE-----
  j
  k
  l
  -----END CERTIFICATE-----
  some other data below this
  </code></pre>
  <p><strong>Expected output :</strong></p>
  <pre><code>-----BEGIN CERTIFICATE-----
  j
  k
  l
  -----END CERTIFICATE-----
  </code></pre>
  <p>Output should include the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----</p>
  <p><strong>My Approach :</strong></p>
  <p>I have tried the below command but it is giving me the output without -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----</p>
  <pre><code>openssl s_client -showcerts -connect localhost:443 &lt;/dev/null | sed -n 'H; /^-----BEGIN CERTIFICATE-----/h; ${g;p;}' |sed -e '1d' -e '/-----END CERTIFICATE-----/q' |sed '$ d' &gt; mycertfile.pem
  </code></pre>
  <p><strong>output from above command :</strong></p>
  <pre><code>j
  k
  l
  </code></pre>