====== Certificates - Split a .pem file ======
cat $file|awk 'split_after==1{n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} {print > "cert" n ".pem"}'
This can leave an empty file if there's a blank line at the end, such as with
openssl pkcs7 -outform PEM -in my-chain-file -print_certs
To prevent that, check the length of the line before printing:
cat $file|awk 'split_after==1{n++;split_after=0}
/-----END CERTIFICATE-----/ {split_after=1}
{if(length($0) > 0) print > "cert" n ".pem"}'
**NOTE:** The awk snippet works for extracting the different parts, but you still need to know which section is the key / cert / chain.
----
===== Using csplit =====
csplit -f cert- $file '/-----BEGIN CERTIFICATE-----/' '{*}'