Issue
How do you pass a variable containing slashes as a pattern to sed
?
For example, if I have the following variable:
var="/Users/Documents/name/file"
I want to pass it to sed
as so:
sed "s/$var/replace/g" "$file"
However I get errors. How can I circumvent the issue?
Solution
Use an alternate regex delimiter as sed
allows you to use any delimiter (including control characters):
sed "s~$var~replace~g" $file
As mentioned above we can use control character as delimiter as well like:
sed "s^[$var^[replace^[g" file
Where ^[
is typed by pressing Ctrl-V-3 together.
Answered By - anubhava