Home > Blockchain >  Problem with replacing a string with SSL cetificate content
Problem with replacing a string with SSL cetificate content

Time:02-15

I have a requirement to replace a string in a file with below certificate content and it needs to be in this format. I am unable to do this with sed in bash.

"-----BEGIN CERTIFICATE-----/r/n"
"MIIDazCCAlOgAwIBAgIUb14HAcAizLZ3jFxFhAQHLdsCWI0wDQYJKoZIhvcNAQEL/r/n"
"BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM/r/n"
"GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMjAyMTQxNzAxMTBaFw0yNzAy/r/n"
"MTMxNzAxMTBaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw/r/n"
"HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB/r/n"
"AQUAA4IBDwAwggEKAoIBAQC55VnblFue90NYNhz15aQBWsZ5FC6EXZibiHwKnE13/r/n"
"j0pCDxrY6oxia6Zz7pZcFyRYrQ4Gd/nconYs5lreijxErG30HgX owdYeW1sdhTW/r/n"
"xyA1hGPQFmMAxs29 5siiZz08QRrEuKDsYqvqQa0Zqrw0hqNVD5Vu74y0U8k12Y0/r/n"
"7kCxQ1GbkZJ4hsVaMOvbIl061f Uk6McUKraXdM45AsgBGZFBVuzrR3QeAnvuIXH/r/n"
"9sMi4vpzBQI1RGaZME1jn4nE5Gy3KM4tn99h4of5ei78iViNoB1ww9orYz2xqnKn/r/n"
"HT0Z m0X94RzAXXAxBW/a7TC8miNn3b3c4hUx/J7rXq3AgMBAAGjUzBRMB0GA1Ud/r/n"
"DgQWBBQisnLWumDdji5PGyhgpz NjNst8jAfBgNVHSMEGDAWgBQisnLWumDdji5P/r/n"
"Gyhgpz NjNst8jAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBi/r/n"
"O4cYreKRE xHprpcBNC3MZh/N1W1rbyAZgoJ3Rg1LkAfjK3gWyu  kEEJ7kENkyS/r/n"
"YaVhJ9lESwLIzhTwnk4ssu0aGtlW2jRktA2yAwaeqbAMbYKdNVGGR/KtXKufM4O6/r/n"
"CKDSyNCxkpyx1j1bOAkv3XUYLfFo9Ze1eGGOltgWvwfogLhGoH xTk LImyBHBE1/r/n"
"5WtfhV0B1 V1hxX1uAuuzleiOfCJnGnSny 9EaQNtTuoo20te1VJbkzh11gIQvoc/r/n"
"pOMSjWAZSSsK/gEI8rpDZwCR34FJM4iMoiYvFnBdwLX1wxXfKyEqfyQtD9nwKt4J/r/n"
"DyOZAEmx 0JxTALodX3V/r/n"
"-----END CERTIFICATE-----/r/n"

I stored this into a variable like below and trying to replace using sed command like below.

  1. export mycert=$(cat mycert.pem)
  2. sed "s/DUMMY/$mycert/g" testfile

I get the errors like below: sed: -e expression #1, char 37: unknown option to `s'

sed: -e expression #1, char 39: unterminated `s' command

I tried almost all combinations I know. No luck

CodePudding user response:

Using GNU sed:

mycert=$(sed '$!s/$/\\/' mycert.perm)
sed "s=DUMMY=$mycert=" testfile
  • Use sed to escape new lines in the replacement, except the last, which is stripped by the command sub.
  • These will be used in the replacement string (because they are escaped).
  • Change the delimiter to =, which doesn't conflict.

CodePudding user response:

Try it please:

cat mycert.pem | sed -i -e 's/^/"/' | sed -i -e 's/.$/\/r\/n"/'
  • Related