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.
You have to check the pattern space and keep pulling in the `N`ext line if it doesn't match e.g.

```
sed '/text = "/{              # if line matches text = "
:b                            # label b
$!N                           # pull in the next line (if not the last one)
/"$/!bb                       # if pattern space doesn't end with " go to label b
s/".*"/""/                    # else remove everything between the quotes
}' infile

```

with `gnu sed` you can write it as

```
sed '/text = "/{:b;$!N;/"$/!bb;s/".*"/""/}' infile

```

That's not very efficient though, better just select the range `/text = "/,/"/`, modify the first line and delete the rest:

```
sed '/text = "/,/"/{            # in this range
/text = "/!d                    # delete all lines not matching text = "
s/\\/"/                         # replace the backslash with quotes (this is only
}' infile                       # executed if the previous d wasn't executed)

```

again, with `gnu sed` you can write it as a one-liner:

```
sed '/text = "/,/"/{/text = "/!d;s/\\/"/}' infile

```