I suggest previous to feed the contents of $var
to the sed script, to escape all the /
chars to be \/
, as in:
var_esc=$(echo "$var" | sed 's/\//\\\//g')
But the sed
expression gets very complicated (you must use single quotes, if you don't want to double \
chars, as double quotes do interpret also the backslashes.
Another thing that could simplify the expression is to use the possibility of change the regexp delimiter (with using a different char to begin it) as in:
var_esc=$(echo "$var" | sed 's:/:\/:g')
The idea is to, before substituting the $var
variable, to escape all the possible interferring chars you can have (you don't know a priory if there are going to be, as you are using a variable for that purpose) and then substitute them in the final command:
sed "s/$var_esc/#line_removed/g"
Finally, if you don't want to substitute the line, but just to erase it, you can do something like this:
sed "/$var_esc/d"
and that will erase all lines that match your $var_esc
contents. (this time I believe you cannot change the regexp delimiter, as the /
introduces the matching line operator --- d
is the command to delete line in the output)
Another way to delete the line in your file is to call ex
directly and pass the editor command as input:
ex file <<EOF <-- edit "file" with the commands that follow until EOF is found.
/$var_esc <-- search for first occurrence of $var_esc
d <-- delete line
w <-- write file
EOF <-- eof marker.