Skip to content

Instantly share code, notes, and snippets.

@trajakovic
Created October 23, 2013 11:36
Split SSL PEM multiple certificates into files
wget -O - http://curl.haxx.se/ca/cacert.pem | awk 'split_after==1{n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} {print > "cert" n ".pem"}'
@apogrebnyak
Copy link

Thanks for providing this sample, the modification below makes sure that all files have numbers starting with 0, and if there are no certificates in the file no output is created.

awk '
BEGIN {n=0;seen_cert=0}
split_after==1                {n++;split_after=0}
/^-----END CERTIFICATE-----$/ {split_after=1;seen_cert=1}
seen_cert==1                  {print > "trust_root_certificate_" n}
' /path/to/certs.pem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment