我想要一個簡單的命令(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-----
我想把這個文件分成2個文件。
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和line的範圍內-----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+
jDXqxxuLE4xlX0YV91LuBN9Re3Y3EnJjpyXR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V2jVFwsXwKS63/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+7EClAMbN3F26xl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4oj+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
)。