A simple find and replace without using any regex (bash)

Hi,

I need to do an exact find and replace (I don't want to use regular expressions because the input comes from user). I want to find a line that matches the user's input text and replace it with an empty string.

For example, let's say the user enters I love "Unix" and the contents of the file where I want to do find and replace is this:

I like "Unix" more than DOS
I love "Unix"
I said I love "Unix"
I love "Unix" a lot

Now I want to replace the line I love "Unix" with empty string. And I want to leave the remaining lines like I said I love "Unix" and I love "Unix" a lot as is. The double-quotes are intentional.

Which Unix utility can do this? An example would be great! I searched and found that sed is something close what I am looking for, but it takes a regular expression. Because the input string is coming from the user, it's tough for me to generate a regular expression for that. Any other ways?

Thanks!

man grep

read input
fgrep -vx "$input" file

It removes the line completely, and doesn't even leave the newline. If you require the newline to be preserved, it's a bit harder.

It's not too tough to generate a proper regular expression, per se, but it tends to be a bit ugly.

read input
echo "$input" | sed -e 's/[][\\.*^$]/\\&/g; s/.*/s%^&\$%%/' | sed -f - file

This is untested, and intended merely as a proof of concept. I probably forgot one or two special characters which need to be backslash-escaped. (Hint: remove the final part of the pipeline to see the generated script.)

Oh, and if you are really strict about bash, try this:

read input
while read line; do
  case $line in
    $input) echo ;;  # or not, if you want the newline gone, too
    *) echo "$line";;
  esac
done <file