我想要一个简单的命令(AWK、Perl 等)将文件拆分成两个文件,如下所示。
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-----
我想将这个文件拆分成两个文件。
filename.key
应包含以下内容:
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALq6EmgljkwfHKkB
4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4vYFSS
8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3Yeju
hIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAECgYBL6GynLe1FALjT89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1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4v
YFSS8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3
YejuhIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAGjUDBOMB0GA1UdDgQWBBRjvXHC
pMQrNHm4bj7e6MJZcUyy+TAfBgNVHSMEGDAWgBRjvXHCpMQrNHm4bj7e6MJZcUyy
+TAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBADegC439XYCfiq4oj+Lm
pH71WtPmbbB2+exEkIYioB8i/mQqIn65sKAVf8pcockoMNlYnvhPPdVfrB81iA1V
2QbzJs3C+1eKhFeOpL2dXrJYD9X5LsSTdOvDcHSa7pkY3CaM2u1JZB6DkYQU81fN
NufoWfmwHFQl64eO3qNys8ks
-----END CERTIFICATE-----
答案1
使用 AWK:
awk '/^-----BEGIN PRIVATE KEY-----$/, /^-----END PRIVATE KEY-----$/ {print >"filename.key"; next} {print >"filename.cert"}' test.pem
扩展的 AWK 脚本:
/^-----BEGIN PRIVATE KEY-----$/, /^-----END PRIVATE KEY-----$/ {
print >"filename.key";
next
}
{
print >"filename.cert"
}
-----BEGIN PRIVATE KEY-----
如果当前行在 a和行的范围内-----END PRIVATE KEY-----
,则打印该行到filename.key
并跳至下一行;否则,打印该行到filename.cert
。
% cat test.pem
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALq6EmgljkwfHKkB
4s1BkXeYE/j0zO271m8IFerHyGVKjeq+RNJ/8vTNaeQSdpTfeGc5kTuNlK4vYFSS
8V2jVFwsXwKS63/H6sfzeFvgaFMJb/DghejxylajQ+71QXwsRdMmPJwH0JU3Yeju
hIA+NegFSU5BVbks8QZeGOeKQ0MlAgMBAAECgYBL6GynLe1FALjT89JRgsAyD/6+
jDXqxxuLE4xlX0YV91LuBN9Re3Y3EnJjpyXRq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2jVFwsXwKS63/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+7EClAMbN3F26xl0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oj+Lm
pH71WtPmbbB2+exEkIYioB8i/mQqIn65sKAVf8pcockoMNlYnvhPPdVfrB81iA1V
2QbzJs3C+1eKhFeOpL2dXrJYD9X5LsSTdOvDcHSa7pkY3CaM2u1JZB6DkYQU81fN
NufoWfmwHFQl64eO3qNys8ks
-----END CERTIFICATE-----
答案2
您还可以使用两行grep
:
grep -Pzo '(?s)-{5}BEGIN (PRIVATE KEY)-{5}.*?-{5}END \1-{5}' test.pem > filename.key
grep -Pzo '(?s)-{5}BEGIN (CERTIFICATE)-{5}.*?-{5}END \1-{5}' test.pem > filename.cert
该选项-P
启用 Perl 正则表达式,-z
将换行符视为普通字符,并且-o
仅打印匹配的部分,而不是整行(因为-z
导致整个输入被视为单行)。
在正则表达式模式中,开关(?s)
导致.
也匹配换行符。
-{5}BEGIN (PRIVATE KEY)-{5}
匹配块标题-----BEGIN PRIVATE KEY-----
。
.*?
匹配块内容(密钥/证书数据)。
-{5}END \1-{5}
匹配块结尾。\1
被评估为来自上方圆括号中的字符串(PRIVATE KEY
或CERTIFICATE
)。