我的cert.enc文件如下。它有多行
MIIPZAIBAzCCDx4GCSqGSIb3DQEHAaCCDw8Egg8LMIIPBzCCBZQGCSqGSIb3DQEH
AaCCBYUEggWBMIIFfTCCBXkGCyqGSIb3DQEMCgECoIIE+jCCBPYwKAYKKoZIhvcN
AQwBAzAaBBTQnaS1AE0gnwlbnfZsLfdzGxXnTAICBAAEggTIhChZpcY/STZNt1kl
1WzBoq5OI+QaFBJFA9nimyMaB3cywcL8ohKbuhHpUzc7jWvfw4iA/V9JDVkW3FHs
CQRQDCYWwzcbm43vkL61+zba133ZaGV4AVMj/RYYwA2lgFAG2/+gKi7ByrdaYiuH
dWSI/Aid+ZNsnO4rx8/BkHAnmKZ+B71bD+Rod4pVGOmW/vwI6lHnYfospcptc3aP
eKa6S1UoK1QHDOplkp1HFZhkiBXxnhIKpbpvvzH0ZoFSiUPlc9ycFZcS3VC1kxM1
AHmqlqecSP+p+FiksnkppCS49quSyoU2IPatkVJTDKpnehLIXj1UIwKCkIcTR6np
1JkQeJXzvIH1yo7IVyUuTVUb2kx3U/OXveeyoRFaODQlt4WNY2Z7cUR+Ub+A/ECv
0hKEIU/HD+g+S93di4n3q5o0w0mHNPXITkHHNgpDW+lS3kHm3IV+8gchP99r//Xx
这是我的XML file.cat test.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PayloadContent</key>
<array>
<dict>
<key>Password</key>
<string>P@ssw0rd</string>
<key>PayloadCertificateFileName</key>
<string>tin.user_11:22:33:44:55:66.p12</string>
<key>PayloadContent</key>
<data>
**PRIVATEKEY**
</data>
<key>PayloadDescription</key>
<string>Adds a PKCS#12-formatted certificate</string>
<key>PayloadDisplayName</key>
<string>tin.user_11:22:33:44:55:66.p12</string>
<key>PayloadIdentifier</key>
<string>com.apple.security.pkcsAF0F-4BFB-A054-FB31E99420A2</string>
<key>PayloadType</key>
<string>com.apple.security.pkcs12</string>
<key>PayloadUUID</key>
<string>AF0F-4BFB-A054-FB31E99420A2</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
我的问题是如何使用cat cert.enc
和<data>
</data>
的输出
以下命令有效
sed '/PRIVATEKEY/r cert.enc' test.xml |sed '/PRIVATEKEY/d'
,但是键位于左边缘。与<data>
位置不同。
答案 0 :(得分:1)
key="$(cat cert.enc)"
xmlstarlet edit --update '//data' --value "$key" test.xml
输出:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PayloadContent</key>
<array>
<dict>
<key>Password</key>
<string>P@ssw0rd</string>
<key>PayloadCertificateFileName</key>
<string>tin.user_11:22:33:44:55:66.p12</string>
<key>PayloadContent</key>
<data>MIIPZAIBAzCCDx4GCSqGSIb3DQEHAaCCDw8Egg8LMIIPBzCCBZQGCSqGSIb3DQEH
AaCCBYUEggWBMIIFfTCCBXkGCyqGSIb3DQEMCgECoIIE+jCCBPYwKAYKKoZIhvcN
AQwBAzAaBBTQnaS1AE0gnwlbnfZsLfdzGxXnTAICBAAEggTIhChZpcY/STZNt1kl
1WzBoq5OI+QaFBJFA9nimyMaB3cywcL8ohKbuhHpUzc7jWvfw4iA/V9JDVkW3FHs
CQRQDCYWwzcbm43vkL61+zba133ZaGV4AVMj/RYYwA2lgFAG2/+gKi7ByrdaYiuH
dWSI/Aid+ZNsnO4rx8/BkHAnmKZ+B71bD+Rod4pVGOmW/vwI6lHnYfospcptc3aP
eKa6S1UoK1QHDOplkp1HFZhkiBXxnhIKpbpvvzH0ZoFSiUPlc9ycFZcS3VC1kxM1
AHmqlqecSP+p+FiksnkppCS49quSyoU2IPatkVJTDKpnehLIXj1UIwKCkIcTR6np
1JkQeJXzvIH1yo7IVyUuTVUb2kx3U/OXveeyoRFaODQlt4WNY2Z7cUR+Ub+A/ECv
0hKEIU/HD+g+S93di4n3q5o0w0mHNPXITkHHNgpDW+lS3kHm3IV+8gchP99r//Xx</data>
<key>PayloadDescription</key>
<string>Adds a PKCS#12-formatted certificate</string>
<key>PayloadDisplayName</key>
<string>tin.user_11:22:33:44:55:66.p12</string>
<key>PayloadIdentifier</key>
<string>com.apple.security.pkcsAF0F-4BFB-A054-FB31E99420A2</string>
<key>PayloadType</key>
<string>com.apple.security.pkcs12</string>
<key>PayloadUUID</key>
<string>AF0F-4BFB-A054-FB31E99420A2</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
</array>
</dict>
</plist>
如果要就地编辑文件,请添加选项-L
。
答案 1 :(得分:0)
这可能对您有用(GNU sed):
sed '/\*\*PRIVATEKEY\*\*/r keyFile' file |
sed -r '/\*\*PRIVATEKEY\*\*/{h;d};/^\S+{64}$/{G;s/(.*)\n(\s*).*/\2\1/}'
这使用与上面所述相同的方法,但是我们不只是删除PRIVATEKEY
,而是制作一个副本,然后删除并将该副本附加到密钥的每一行。然后使用模式匹配,将缩进移到键的前面。
替代方法:
sed -e '/\*\*PRIVATEKEY\*\*/r keyFile' -e '//d' file |
sed '/<data>/h;//,/<\/data/{//b;G;s/\(.*\)\n\(\s*\).*/\2\1/}'
使用data
标签范围来修改缩进,而不是依赖键的长度。