Dividir o arquivo em dois arquivos quando o padrão aparecer usando o comando

5

Eu quero um comando simples (AWK, Perl, etc.) para dividir o arquivo em dois arquivos, conforme mostrado abaixo.

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-----

Eu quero dividir este arquivo em 2 arquivos.

filename.key deve conter o conteúdo abaixo:

-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALq6EmgljkwfHKkB
4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4vYFSS
8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJw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c2NwbHAwNjUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALq6Emgljkwf
HKkB4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4v
YFSS8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3
YejuhIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAGjUDBOMB0GA1UdDgQWBBRjvXHC
pMQrNHm4bj7e6MJZcUyy+TAfBgNVHSMEGDAWgBRjvXHCpMQrNHm4bj7e6MJZcUyy
+TAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBADegC439XYCfiq4oj+Lm
pH71WtPmbbB2+exEkIYioB8i/mQqIn65sKAVf8pcockoMNlYnvhPPdVfrB81iA1V
2QbzJs3C+1eKhFeOpL2dXrJYD9X5LsSTdOvDcHSa7pkY3CaM2u1JZB6DkYQU81fN
NufoWfmwHFQl64eO3qNys8ks
-----END CERTIFICATE-----
    
por sra1 17.03.2016 / 08:22

2 respostas

3

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 em filename.key e pula para a próxima linha; caso contrário, imprime a linha em filename.cert .
% cat test.pem 
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALq6EmgljkwfHKkB
4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4vYFSS
8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0J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IhvcNAQEBBQADgY0AMIGJAoGBALq6Emgljkwf
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+m0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MJZcUyy+TAfBgNVHSMEGDAWgBRjvXHCpMQrNHm4bj7e6MJZcUyy
+TAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBADegC439XYCfiq4oj+Lm
pH71WtPmbbB2+exEkIYioB8i/mQqIn65sKAVf8pcockoMNlYnvhPPdVfrB81iA1V
2QbzJs3C+1eKhFeOpL2dXrJYD9X5LsSTdOvDcHSa7pkY3CaM2u1JZB6DkYQU81fN
NufoWfmwHFQl64eO3qNys8ks
-----END CERTIFICATE-----
    
por kos 17.03.2016 / 08:36
2

Você também pode usar duas linhas de grep :

grep -Pzo '(?s)-{5}BEGIN (PRIVATE KEY)-{5}.*?-{5}END -{5}' test.pem > filename.key
grep -Pzo '(?s)-{5}BEGIN (CERTIFICATE)-{5}.*?-{5}END -{5}' test.pem > filename.cert

As opções -P habilitam regexes Perl, -z trata caracteres de nova linha como caracteres normais e -o apenas imprime a parte correspondente, não a linha inteira (pois -z faz com que toda a entrada seja tratada como única linha).

No padrão de expressão regular, a opção (?s) faz com que . também corresponda a caracteres de nova linha.
-{5}BEGIN (PRIVATE KEY)-{5} corresponde ao título de bloco -----BEGIN PRIVATE KEY----- .
.*? corresponde ao conteúdo do bloco (a key / certificate data).% -{5}END -{5} corresponde ao final do bloco. O é avaliado para a string entre colchetes acima ( PRIVATE KEY ou CERTIFICATE ).

    
por Byte Commander 17.03.2016 / 08:56