-1

cat file.txt

This is my CRT.

I am taking the certificate input from user using read command.

Below is my input.

-----BEGIN CERTIFICATE-----
\r\nGGHIDqjCCApICCQDbD5hdEvLLsTANBgkqhkiG9w0BAQsFADCBkTELPOAkGA1UEBhMC\r\nU0ExGTAXBgNVBAgMEEVhc3Rlcm4gUHJvdmluY2UxEjAQBgXBhnhfdxd/liuffgfgseeadR4DmKACPesFIiiiqEKi9ouJtDY7mg7I7\r\n8vPudZ2Qs6x9F/i6/1WBz1UoZBT//gHYYRIBsOUCGwxZJCs1Lcy2zMWf1Um\r\n-
----END CERTIFICATE-----

How can I replace this CRT with this input certificate. I tried sed "s/CRT/$INPUT/" file_name but that is not working.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Rahul
  • 49
  • 1
  • 5
  • 1
    `replaceEscaped=$(sed 's/[&/\]/\\&/g' <<< "$INPUT"); sed "s/CRT/$replaceEscaped/" file_name`. See [Is it possible to escape regex metacharacters reliably with sed](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) – Wiktor Stribiżew Apr 16 '21 at 12:24
  • @Wiktor, I think you should add this an an answer as I think it does what Rahul is looking for by maintaining the text `\r\n` in the output. – Mehmet Karatay Apr 16 '21 at 14:31
  • 1
    I do not have to, this answer already [exists](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed). – Wiktor Stribiżew Apr 16 '21 at 14:48
  • Does this answer your question? [Is it possible to escape regex metacharacters reliably with sed](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) – Ryszard Czech Apr 16 '21 at 22:33

1 Answers1

0

Try

sed "s:CRT:$INPUT:" file_name

Your issue is likely to be the slashes / in $INPUT. sed can use multiple characters for the delimiter, so here I've used :, which isn't in $INPUT, bypassing the problem.

This works for me:

INPUT='hello/world\r\n'
echo CRT example | sed "s:CRT:$INPUT:" 

The output is:

hello/world
 example

'example' is on a new line because of the \r\n at the end of $INPUT. If INPUT='hello/world', without the \r\n, then the output is all one line:

hello/world example

See also: Replace a string including a slash “/” using sed command

Mehmet Karatay
  • 269
  • 4
  • 11
  • My input was -----BEGIN CERTIFICATE-----\r\nMIIDqjCCApICCQDbD5hdEvLLsTANBgkqhkiG9w0BAQsFADCBkTELMAkGA1UEBhMC\r\nU0jui/jgdffjTYUFmluY2UxEjAQBgNVBAcMCUFsIEtob2Jh\r\ncjEvMC0GUm\r\n-----END CERTIFICATE----- but i am not seeing \r\n in my output when I used sed "s:CRT:$INPUT:" – Rahul Apr 16 '21 at 11:48
  • Are you seeing a new line after '-----BEGIN CERTIFICATE-----' or is everything on the same line? `\r\n` is the Window's new line character, so a text editor will simply show that as a new line. – Mehmet Karatay Apr 16 '21 at 11:54
  • @Rahul, I've added an example of what I mean about seeing a new line to the answer. – Mehmet Karatay Apr 16 '21 at 12:10
  • I want the exact value of Input i.e including \r\n as text. – Rahul Apr 16 '21 at 13:50
  • It's down to each programme you use how the `\r\n` is interpreted; it's nothing to do with sed. Those characters *are* there, it's just the programming displaying your text file is *choosing* not to show them and putting in a new line instead. If you copy and paste, those characters will get copied as well. See: [Text Editor which shows \r\n?](https://stackoverflow.com/questions/1446370/text-editor-which-shows-r-n). Exercise: In your terminal, try typing `echo $INPUT` and see what comes out; I don't think your terminal will show your the `\r\n` either. – Mehmet Karatay Apr 16 '21 at 13:59
  • See [Control Characters](https://en.wikipedia.org/wiki/Control_character) on Wikipedia. – Mehmet Karatay Apr 16 '21 at 14:05