Question: I have a large file which has special characters in it. There is a multi line code there, that I want to replace with `sed`. 

This:

```
  text = "\
    ------                                                           ------\n\n\
    This message was automatically generated by email software\n\
    The delivery of your message has not been affected.\n\n\
    ------                                                           ------\n\n"

```

Needs to turn into this:

```
text = ""

```

I tried the following code, but no luck:

```
sed -i '/  text = "*/ {N; s/  text = .*affected.\./  text = ""/g}' /etc/exim.conf

```

It does not replace anything and does not display any error messages

I have been playing with it, but everything I try does not work.

Answer: 
Perl to the rescue:

```
perl -i~ -0777 -pe 's/text = "[^"]+"/text = ""/g' input-file

```

* `-i~` will edit the file "in place", leaving a backup copy
* `-0777` reads the whole file at once, not line by line

The substitution `s///` works similarly as in sed (i.e. it matches `text = "` followed by anything but double quotes many times up to a double quote), but in this case, it works on the whole file.
Personally, I would do this in Perl. If we can assume that there are no `"` before the closing `"`, you can do:

```
perl -0pe 's/(text\s*=\s*)".*?"/$1""/s' file

```

The `-0` slurps the entire file, reading it into memory. The `-p` means "print every line (here, a "line" will be the entire file) after applying the script given by `-e`". The script itself is a simple substitution operator. It will capture the string `text` followed by 0 or more whitespace characters, an `=` and 0 or more whitespace again (`text\s*=\s*`) and save it as `$1`. Then, it will replace the captured pattern as well as the shortest quoted string it finds with the pattern (`$1`) and `""`. The `s` flag makes `.` match newlines.