Usando o AWK:
awk '/^-----BEGIN PRIVATE KEY-----$/, /^-----END PRIVATE KEY-----$/ {print >"filename.key"; next} {print >"filename.cert"}' test.pem
Script AWK expandido:
/^-----BEGIN PRIVATE KEY-----$/, /^-----END PRIVATE KEY-----$/ {
print >"filename.key";
next
}
{
print >"filename.cert"
}
- Se a linha atual estiver no intervalo de uma linha
-----BEGIN PRIVATE KEY-----
e-----END PRIVATE KEY-----
, imprime a linha emfilename.key
e pula para a próxima linha; caso contrário, imprime a linha emfilename.cert
.
% cat test.pem
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALq6EmgljkwfHKkB
4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4vYFSS
8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3Yeju
hIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAECgYBL6GynLe1FALjT89JRgsAyD/6+
jDXqxxuLE4xlX0YV91LuBN9Re3Y3EnJjpyXRq27097AUt5+jl9rfRsUcPdOC2s4W
txAEdJ2INgRbgilu2jgrKItPT9fC2e1oRJoHYIPHmE1SbXEqjm5fmelG7dqoNv95
Y/yr/YvIuF/aC8AEmQJBAOpYD0y5tr7wi0SXH2EWYlg4u+9++o14e+m0LDA8KbbL
uraS3XrHsS9IfKTLlJcyxTMQDA3TzYOfOAECs/hWlhcCQQDL+4Uz8yPVPkxoWFpH
qAauQ/kvxMQLRgJzc9+7EClAMbN3F26xl0GMmNE8eQ1A3622HG2ejMdgS07VOxOJ
rfIjAkEA3aACeDF63wFm9Ktnhc5Bm+Wr90hDdN/pcJBw6f08BiCOqHQk+iU5MNqA
5umR0Zy2WyBmBf21czF1KZ+AzPA2aQJAElEis/iIZjSy7cfJS5f2CZ4G+Vgk9pIx
1CY6l/Sqj54m7HHJOd9IPpTWbSS9oO61DZ5d5Zfkq2v3x0QHzBw3mwJBAKN1xO/3
RkwGrcgBUmviRpZN2GT6uNQd4lV1rLQXoPRqUUeFA7tSD198EXAiRGdMFMRBr+c6
C8ugBRjNMLUicy8=
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIICfjCCAeegAwIBAgIJANykL9YOhRqKMA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV
BAYTAklOMRMwEQYDVQQIDApUYW1pbCBOYWR1MRAwDgYDVQQHDAdDaGVubmFpMQww
CgYDVQQKDANGQ0kxFDASBgNVBAMMC3J0eHNjcGxwMDY1MB4XDTE2MDMxNjE2MDUy
OVoXDTE3MDMxNjE2MDUyOVowWDELMAkGA1UEBhMCSU4xEzARBgNVBAgMClRhbWls
IE5hZHUxEDAOBgNVBAcMB0NoZW5uYWkxDDAKBgNVBAoMA0ZDSTEUMBIGA1UEAwwL
cnR4c2NwbHAwNjUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALq6Emgljkwf
HKkB4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4v
YFSS8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3
YejuhIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAGjUDBOMB0GA1UdDgQWBBRjvXHC
pMQrNHm4bj7e6MJZcUyy+TAfBgNVHSMEGDAWgBRjvXHCpMQrNHm4bj7e6MJZcUyy
+TAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBADegC439XYCfiq4oj+Lm
pH71WtPmbbB2+exEkIYioB8i/mQqIn65sKAVf8pcockoMNlYnvhPPdVfrB81iA1V
2QbzJs3C+1eKhFeOpL2dXrJYD9X5LsSTdOvDcHSa7pkY3CaM2u1JZB6DkYQU81fN
NufoWfmwHFQl64eO3qNys8ks
-----END CERTIFICATE-----
% awk '/^-----BEGIN PRIVATE KEY------$/, /^-----END PRIVATE KEY------$/ {print >"filename.key"; next} {print >"filename.cert"}' test.pem
% cat filename.key
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALq6EmgljkwfHKkB
4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4vYFSS
8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3Yeju
hIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAECgYBL6GynLe1FALjT89JRgsAyD/6+
jDXqxxuLE4xlX0YV91LuBN9Re3Y3EnJjpyXRq27097AUt5+jl9rfRsUcPdOC2s4W
txAEdJ2INgRbgilu2jgrKItPT9fC2e1oRJoHYIPHmE1SbXEqjm5fmelG7dqoNv95
Y/yr/YvIuF/aC8AEmQJBAOpYD0y5tr7wi0SXH2EWYlg4u+9++o14e+m0LDA8KbbL
uraS3XrHsS9IfKTLlJcyxTMQDA3TzYOfOAECs/hWlhcCQQDL+4Uz8yPVPkxoWFpH
qAauQ/kvxMQLRgJzc9+7EClAMbN3F26xl0GMmNE8eQ1A3622HG2ejMdgS07VOxOJ
rfIjAkEA3aACeDF63wFm9Ktnhc5Bm+Wr90hDdN/pcJBw6f08BiCOqHQk+iU5MNqA
5umR0Zy2WyBmBf21czF1KZ+AzPA2aQJAElEis/iIZjSy7cfJS5f2CZ4G+Vgk9pIx
1CY6l/Sqj54m7HHJOd9IPpTWbSS9oO61DZ5d5Zfkq2v3x0QHzBw3mwJBAKN1xO/3
RkwGrcgBUmviRpZN2GT6uNQd4lV1rLQXoPRqUUeFA7tSD198EXAiRGdMFMRBr+c6
C8ugBRjNMLUicy8=
-----END PRIVATE KEY-----
% cat filename.cert
-----BEGIN CERTIFICATE-----
MIICfjCCAeegAwIBAgIJANykL9YOhRqKMA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV
BAYTAklOMRMwEQYDVQQIDApUYW1pbCBOYWR1MRAwDgYDVQQH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