Bash: How to delete a multi-line string which may contain special characters from a string/file

Viewed 34

I'm writing a script that at one point needs to remove a literal string (no regex matching needed) from a file. Both the string and file path are in script variables.

The problem is that the string is multi-line, and may also contain special characters. Additionally, not all lines in the string are unique in the file (but the string as a whole is), so I cannot go line by line to delete each individual one from the file.

For example, when I echo "$stringToDelete" from my script I get something like:

values(
  /the/@quick/[]/"fox",
  //jumped/over/the,
  //lazy/{},
)

I've tried some approaches using sed and awk but any attempt fails with issues around either the newlines or the special characters. I've seen some answers invoking perl but couldn't get that to work.

Also I can easily read the full file's content into a variable, so the solution doesn't need to edit the file directly - a way to remove the stringToDelete from another string var is fine too.

0 Answers
Related