0

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

Vaishnav
  • 475
  • 1
  • 4
  • 17
  • why is it in that format? each line is not wrapped in quotes and it's `\r\n` not `/r/n`, its wrong – Lawrence Cherone Feb 14 '22 at 19:07
  • I need it in that format and yes it is /r/n. It is suppose to be that way. The content cannot be manipuated – Vaishnav Feb 14 '22 at 19:14
  • 1
    Does this answer your question? [Use the contents of a file to replace a string using SED](https://stackoverflow.com/questions/6790631/use-the-contents-of-a-file-to-replace-a-string-using-sed); also see [this](https://askubuntu.com/q/540532); eg: `sed -e "/DUMMY/r mycert.pem" -e "/DUMMY/d" testfile` – markp-fuso Feb 14 '22 at 19:28
  • Somebody is doing something horribly wrong if you have the backslashes the wrong way. – tripleee Feb 15 '22 at 06:08

2 Answers2

2

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.
dan
  • 3,701
  • 4
  • 11
1

Try it please:

cat mycert.pem | sed -i -e 's/^/"/' | sed -i -e 's/.$/\/r\/n"/'
Juranir Santos
  • 320
  • 2
  • 5
  • Thanks lot for checking @Juranir but that did not help. Thanks once again for your time. The result is not the intented one but yet some modifications if made might help. +1 for that :) – Vaishnav Feb 14 '22 at 19:51