Skip to content

Instantly share code, notes, and snippets.

@cxhercules
Forked from jinnko/pem-split
Created August 23, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxhercules/d82e842cbcf10e5e5788d6c071c1ecde to your computer and use it in GitHub Desktop.
Save cxhercules/d82e842cbcf10e5e5788d6c071c1ecde to your computer and use it in GitHub Desktop.
Take a PEM format file as input and split out certs and keys into separate files.
#!/usr/bin/awk -f
#
# Take a PEM format file as input and split out certs and keys into separate files
#
BEGIN { n=0; cert=0; key=0; if ( ARGC < 2 ) { print "Usage: pem-split FILENAME"; exit 1 } }
/-----BEGIN PRIVATE KEY-----/ { key=1; cert=0 }
/-----BEGIN CERTIFICATE-----/ { cert=1; key=0 }
split_after == 1 { n++; split_after=0 }
/-----END CERTIFICATE-----/ { split_after=1 }
/-----END PRIVATE KEY-----/ { split_after=1 }
key == 1 { print > FILENAME "-" n ".key" }
cert == 1 { print > FILENAME "-" n ".crt" }
@cxhercules
Copy link
Author

Taken from https://serverfault.com/questions/391396/how-to-split-a-pem-file:

awk '
  split_after == 1 {n++;split_after=0}
  /-----END CERTIFICATE-----/ {split_after=1}
  {print > "cert" n ".pem"}' < $file

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