This looks like a job for Range Operators:
use strict; use warnings; while (<DATA>) { print if (/BEGIN RSA PRIVATE KEY/ .. /END RSA PRIVATE KEY/); } __DATA__ Bag Attributes localKeyID: 33 32 34 33 39 38 33 33 30 32 36 30 33 friendlyName: verisign class 3 public primary certification author +ity subject=/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification + Authority issuer=/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification +Authority -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- Bag Attributes localKeyID: 31 32 33 33 39 38 33 33 30 22 11 30 34 friendlyName: mycerts.test.com Key Attributes: <No Attributes> -----BEGIN RSA PRIVATE KEY----- THIS IS THE FILE i WANT ... -----END RSA PRIVATE KEY----- Bag Attributes localKeyID: 23 32 34 33 39 38 33 33 44 32 36 30 21 friendlyName: mycerts.test.com subject=/C=US/ST=Somewhere/L=There/O=My Org/OU=Terms of use at www.ver +isign.com/rpa (c)00/CN=MYCERTS.TEST.COM issuer=/O=VeriSign Trust Network/OU=VeriSign, Inc./OU=VeriSign Interna +tional Server CA - Class 3/OU=www.verisign.com/CPS Incorp.by Ref. LIA +BILITY LTD.(c)97 VeriSign -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- Bag Attributes localKeyID: 31 32 34 33 39 38 44 33 30 32 36 30 36 friendlyName: verisign intermediate ca subject=/O=VeriSign Trust Network/OU=VeriSign, Inc./OU=VeriSign Intern +ational Server CA - Class 3/OU=www.verisign.com/CPS Incorp.by Ref. LI +ABILITY LTD.(c)97 VeriSign issuer=/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification +Authority -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----

which prints:

-----BEGIN RSA PRIVATE KEY----- THIS IS THE FILE i WANT ... -----END RSA PRIVATE KEY-----

In reply to Re: Using Perl to convert pkcs12 certificates to pem format, separate files for public/public keys by toolic
in thread Using Perl to convert pkcs12 certificates to pem format, separate files for public/public keys by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.