Sed - delete line with multiple special characters using a variable

I'm having the following file:

  • test.txt

which contains:

3;/var/tmp/test.mp4
3;/var/tmp/test2.mp4
1;/var/tmp/test3.mp4

I need to remove for example "3;/var/tmp/test2.mp4" using a variable.

string="3;/var/tmp/test2.mp4"
sed -i '#$string#d' test.txt

Does not throw any errors but I can't seem to get the line deleted... Any help would be appreciated.

Thanks!

1 Answer

You have to escape the first delimiter if different than / in the address.

Also if you use variables then you have to expose them to the shell (with double quotes to avoid problems with blanks).

sed -i "\#$string#d" test.txt

or

sed -i '\#'"$string"'#d' test.txt
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like