User Tools

Site Tools


certificates:split_a_.pem_file

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-----/' '{*}'
certificates/split_a_.pem_file.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki