我正在尝试替换获取crt
文件的内容并将json文件中的字符串替换为检索到的crt内容,这就是我所做的:
-----BEGIN CERTIFICATE-----
MIICNTCCAdygAwIBAgIRALrbz2i0JY1TqgQkKLp9dxEwCgYIKoZIzj0EAwIwbDEL
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG
cmFuY2lzY28xFDASBgNVBAoTC2V4YW1wbGUuY29tMRowGAYDVQQDExF0bHNjYS5l
eGFtcGxlLmNvbTAeFw0xODA1MjMwNDM0MDBaFw0yODA1MjAwNDM0MDBaMGwxCzAJ
BgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJh
bmNpc2NvMRQwEgYDVQQKEwtleGFtcGxlLmNvbTEaMBgGA1UEAxMRdGxzY2EuZXhh
bXBsZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASr/tmDhbly94C52F1M
O8CvZdDupDB7sfofihmDg0xsrdfpFy3ylZ4NO5vCfOMIxM/uhLarO8p8uuWNm9iP
oDWSo18wXTAOBgNVHQ8BAf8EBAMCAaYwDwYDVR0lBAgwBgYEVR0lADAPBgNVHRMB
Af8EBTADAQH/MCkGA1UdDgQiBCCCjhsPikle31Kv3ixPqFwL/08aOLU5wb7s7Ucf
RUJLEDAKBggqhkjOPQQDAgNHADBEAiAV/SzA9A7DgONfAbnlaVZzA8ZWcovPp5zI
JYCvxDO8/AIgIj2J6hOpgXxbeuO9kF+srpkwKE8Z1/5Efwd73nMcsvo=
-----END CERTIFICATE-----
{
"cert": "CERT"
}
# Fetching cert.crt content and storing in CERT variable
CERT=$(cat cert.crt)
# Converting cert to single line string by replacing newline with \n
CERT=$(echo "${CERT//$'\n'/\n}")
# Trying to replace CERT string in template.json and store the updated json in test.json
sed -e "s/_CERT_/${CERT}/g" template.json > test.json
运行脚本时出现以下错误:
sed: 1: "s/ORDERER_CA_CERT/----- ...": bad flag in substitute command: 't'
{
"cert": "-----BEGIN CERTIFICATE-----\nMIICNTCCAdygAwIBAgIRALrbz2i0JY1TqgQkKLp9dxEwCgYIKoZIzj0EAwIwbDEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xFDASBgNVBAoTC2V4YW1wbGUuY29tMRowGAYDVQQDExF0bHNjYS5l\neGFtcGxlLmNvbTAeFw0xODA1MjMwNDM0MDBaFw0yODA1MjAwNDM0MDBaMGwxCzAJ\nBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJh\nbmNpc2NvMRQwEgYDVQQKEwtleGFtcGxlLmNvbTEaMBgGA1UEAxMRdGxzY2EuZXhh\nbXBsZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASr/tmDhbly94C52F1M\nO8CvZdDupDB7sfofihmDg0xsrdfpFy3ylZ4NO5vCfOMIxM/uhLarO8p8uuWNm9iP\noDWSo18wXTAOBgNVHQ8BAf8EBAMCAaYwDwYDVR0lBAgwBgYEVR0lADAPBgNVHRMB\nAf8EBTADAQH/MCkGA1UdDgQiBCCCjhsPikle31Kv3ixPqFwL/08aOLU5wb7s7Ucf\nRUJLEDAKBggqhkjOPQQDAgNHADBEAiAV/SzA9A7DgONfAbnlaVZzA8ZWcovPp5zI\nJYCvxDO8/AIgIj2J6hOpgXxbeuO9kF+srpkwKE8Z1/5Efwd73nMcsvo=\n-----END CERTIFICATE-----"
}
答案 0 :(得分:1)
您可以在sed
中使用备用分隔符,因为/
用于证书数据:
cert=$(<cert.crt)
cert="${cert//$'\n'/\\\\n}"
sed "s~\"CERT\"~\"$cert\"~" template.json
替换\\\\n
以获得输出中的单个\n
。
{
"cert": "-----BEGIN CERTIFICATE-----\nMIICNTCCAdygAwIBAgIRALrbz2i0JY1TqgQkKLp9dxEwCgYIKoZIzj0EAwIwbDEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xFDASBgNVBAoTC2V4YW1wbGUuY29tMRowGAYDVQQDExF0bHNjYS5l\neGFtcGxlLmNvbTAeFw0xODA1MjMwNDM0MDBaFw0yODA1MjAwNDM0MDBaMGwxCzAJ\nBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJh\nbmNpc2NvMRQwEgYDVQQKEwtleGFtcGxlLmNvbTEaMBgGA1UEAxMRdGxzY2EuZXhh\nbXBsZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASr/tmDhbly94C52F1M\nO8CvZdDupDB7sfofihmDg0xsrdfpFy3ylZ4NO5vCfOMIxM/uhLarO8p8uuWNm9iP\noDWSo18wXTAOBgNVHQ8BAf8EBAMCAaYwDwYDVR0lBAgwBgYEVR0lADAPBgNVHRMB\nAf8EBTADAQH/MCkGA1UdDgQiBCCCjhsPikle31Kv3ixPqFwL/08aOLU5wb7s7Ucf\nRUJLEDAKBggqhkjOPQQDAgNHADBEAiAV/SzA9A7DgONfAbnlaVZzA8ZWcovPp5zI\nJYCvxDO8/AIgIj2J6hOpgXxbeuO9kF+srpkwKE8Z1/5Efwd73nMcsvo=\n-----END CERTIFICATE-----"
}
以下是使用awk
的单线解决方案:
awk -v cert="$(<cert.crt)" 'BEGIN{gsub(/\n/, "\\n", cert)}
{sub(/"CERT"/, "\"" cert "\"")} 1' template.json
答案 1 :(得分:1)
有一种安全的方法可以直接在jq
中执行此操作:
$ echo '{}' | jq --arg cert "$(<cert.crt)" '{"cert": $cert}'
{
"cert": "-----BEGIN CERTIFICATE-----\nMIICNTCCAdygAwIBAgIRALrbz2i0JY1TqgQkKLp9dxEwCgYIKoZIzj0EAwIwbDEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xFDASBgNVBAoTC2V4YW1wbGUuY29tMRowGAYDVQQDExF0bHNjYS5l\neGFtcGxlLmNvbTAeFw0xODA1MjMwNDM0MDBaFw0yODA1MjAwNDM0MDBaMGwxCzAJ\nBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJh\nbmNpc2NvMRQwEgYDVQQKEwtleGFtcGxlLmNvbTEaMBgGA1UEAxMRdGxzY2EuZXhh\nbXBsZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASr/tmDhbly94C52F1M\nO8CvZdDupDB7sfofihmDg0xsrdfpFy3ylZ4NO5vCfOMIxM/uhLarO8p8uuWNm9iP\noDWSo18wXTAOBgNVHQ8BAf8EBAMCAaYwDwYDVR0lBAgwBgYEVR0lADAPBgNVHRMB\nAf8EBTADAQH/MCkGA1UdDgQiBCCCjhsPikle31Kv3ixPqFwL/08aOLU5wb7s7Ucf\nRUJLEDAKBggqhkjOPQQDAgNHADBEAiAV/SzA9A7DgONfAbnlaVZzA8ZWcovPp5zI\nJYCvxDO8/AIgIj2J6hOpgXxbeuO9kF+srpkwKE8Z1/5Efwd73nMcsvo=\n-----END CERTIFICATE-----"
}