LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sed search for variable and delete entire line, but variable contains forward /'s (https://www.linuxquestions.org/questions/programming-9/sed-search-for-variable-and-delete-entire-line-but-variable-contains-forward-s-682406/)

Passions 11-10-2008 12:20 PM

Sed search for variable and delete entire line, but variable contains forward /'s
 
Hi, I wanted to search for a line and delete it, but the string contains a variable with various number of forward /'s.

path=/some/tmp/directory/that/is/different/each/time

sed "/$path/d" file

won't work. I tried replacing the forward slash, but I believe that's only when searching for strings.

sed "&$path&d" file
sed "%$path%d" file

doesn't work. Any ideas?

TB0ne 11-10-2008 12:49 PM

Quote:

Originally Posted by Passions (Post 3337329)
Hi, I wanted to search for a line and delete it, but the string contains a variable with various number of forward /'s.

path=/some/tmp/directory/that/is/different/each/time

sed "/$path/d" file

won't work. I tried replacing the forward slash, but I believe that's only when searching for strings.

sed "&$path&d" file
sed "%$path%d" file

doesn't work. Any ideas?

Yep...precede the forward slash with a back-slash, like this:

sed "/\//d" file

should do it. The back-slash tells sed that the next thing is only a character, not any sort of 'special' thing (useful for things like $, ^, etc...)

Kenhelm 11-10-2008 03:44 PM

If an alternative to the usual '/' delimiter is used in a sed line range the first one has to be escaped with a '\'. Try:
sed "\%$path%d" file


All times are GMT -5. The time now is 03:51 PM.