0

I have one RSA PRIVATE KEY and one CERTIFICATE in one line, and I have to split it to one .key and .crt file.

Is it possible with sed or/and awk?

EDI1: example (as you can see its literally one line)

-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAoigkOEwqD/d5VsRgCZx8ZjTG+2lMZ+q1QyRaOXZ0OXIFWoKq JlFKI2BauRYl83pM1CLcwrdmnPGGyU9ld5JkWgj9zwI5O7kvyl0sW9ekiDGRdyT+ AEGV5E5aCiki9hgrj61CyvGV4audP3jG+dNi07yFnm40uC1HkrKtFDgiTYMDb2L4 m/UP8pszFiar9rBY0AEhL55gLREX4a/QDxbmP5puUvTErgt0mfbe444+ke5fxxTZ BKhZbVhaPEVVcUVgPlw1EH9wmgj4fTnlNDZBPVqvej8CEt4y/RcEOZ6nPddjBZm+ inr3QbOHc9bvUy5El8RlkqCU/kWkgj1sZlkMGwIDAQABAoIBAG3c6H1EDRN4Rbtl nPOmFBpjE5VuiBe3y0tMK9zWxnPNLpdeA82mMaa6N31C5j+J0te/Lva1r04RzZJC uqpCgr8FbIB+LyypkRl3+v/WoGCtxLKGW5js1pMyCorryAYX5huX9bLt7vBQh19j gtvsPf7asWTfodROyTk42U7joUoFr4TiqBftjVGl3gLB1AidLMHukbiCgRoqGC3v H80VAmc8bt8S46MOvfw1p1Wj/hfyz57pwrU/8/+FHFExzjtkA5Kd8KidzcBc6OnA 3dfsdfsdfdghhhrdfghgfhfghfghhNR0ze6qvlQc6+QQcVIuKxOhmObtBsagnAHJ eqr7FXECgYEAzy881viDqAmsbFC5BN/4GRdUK5nKWFbjD8bo8PbCFMh8Mb+K/GKc dHhUZSMKGQ4u2NNI8fcnh3M0F2zdoaVqZndwr9GxMWyPoB+KBeelDFIWHcY7z55c N2r4CRyWAOMU4lkvBn5RQz1Qxv2de/Tbr3VdxT1tkbWIruIl+wisfwUCgYEAyFz2 xJRugSIBH89haeoHs4JqIKiuHm7+UHkawtfGqrg9jeJDfs7N4hlha5Wf7SDaXMAb Rc8MJGpU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oJT+RaSCPWxmWQwbAgMBAAGjggEnMIIB
IzAOBgNVHQ8BAf8EBAMCA6gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMC
MB0GA1UdDgQWBBTkq4+xrl/ODW8rrp3BdTRw849a9jAfBgNVHSMEGDAWgBRbQpEM
sHkbVhoSNGSHqhu+gJBgWTBRBggrBgEFBQcBAQRFMEMwQQYIKwYBBQUHMAKGNWh0
dHA6Ly9zZWN1cml0eV9wb2xpY3lfbWdsfdssdfdsfdsfdsdsfsdfsdffZWNlcnRz
L2NhMBwGA1UdEQQVMBOCEXdvcmtlcm5vZGUubWljYWRvMEEGA1UdHwQ6MDgwNqA0
oDKGMGh0dHA6Ly9zZWN1cml0eV9wb2xpY3lfbWFuYWdlcjo1MDAzL3YxLjAvbm9k
ZWNybDANBgkqhsdfsdfsdfsdfdsffQEAtnBFseEJBzMGMuqRYeXa0rFGcZUXK4S8
HXvpGU95IX988hlv6d4lKc9gEIFBZrqdSk2Q3+GVy3mAGhRpdwwpY5fwlG6wBlOk
4iQOzMVzjYSdsfdsfsdfsfsdfsfsdfdggtz+6Bc+Rs3o2y9L82O14fYBeDPKKh/1
/VBDvVnGlXjXy8FJWgp4Zfsdfdsfdsfsdfsdfsdfdsdsfddfsdsfsdf0UKplawwP
V9Jh+Ruj0Q1MVPjYisNypyZ5F/snO2v9Tgy9/7FID378ZoWRAHYkMljow+ZSt9pF
+p7GR/BLA+vfcWV2482KeYdsfsdfsdfdsfdsfdsfsdfsfd2Or3Q==
-----END CERTIFICATE-----
  • 1
    It would be easier if you had here the file structure with some altered data. – Rui F Ribeiro Jan 25 at 16:28
  • @RuiFRibeiro You're right, I added example. – G3ph4z Jan 25 at 16:56
3
delimiter='-{5}[^-]+-{5}'
key=1
crt=2

# get the key
grep -o -P "\\${delimiter}.*?${delimiter}" cert | cut -d $'\n' -f $key | fmt -w 64 > the.key

# get the certificate
grep -o -P "\\${delimiter}.*?${delimiter}" cert | cut -d $'\n' -f $crt | fmt -w 64 > the.crt

The output files are the.key and the.crt.

I assume your intention is that .key and .crt are extensions instead of hidden files.

Also, cert is the input file that contains the key and the certificate all in one line.

  • I'm very grateful, you saved my weekend, thank you! – G3ph4z Jan 25 at 17:22
  • @G3ph4z I almost forgot to mention what I named the input file. I've just edited my answer. – Niko Gambt Jan 25 at 17:24
0

Using fmt -w 35 file where file is the single line file, will get it into the exact format that you show in the question. fmt is a simple text formatter that will fold long lines and fmt -w 35 would try to fold the lines in in the file to match 35 characters per line, but would not break apart words that are too long, which is what makes it work in this case.

The awk command

awk 'BEGIN { of="file.pem" } { print >of } /^-----END/ { of="file.crt" }'

would read this and output the first bit to file.pem and the second bit to file.crt. It does this by defining the variable of ("out file") at the start to the first filename, and then changing it to the second filename when a line matching the pattern ^-----END is found. All lines in the output from fmt are printed to whatever filename of happens to contain.

All together:

fmt -w 35 file |
awk 'BEGIN { of="file.pem" } { print >of } /^-----END/ { of="file.crt" }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.