I am trying to avoid a custom search string that can contain any arbitrary character and pass it to sed, but cannot figure out how to make it safe to use sed. In sed, we do s/search/replace/ , and I want to search for exactly the characters in the search string without interpreting their sed (for example, '/' in 'my / path' does not close the sed expression).
I read this related question regarding how to avoid the replacement condition. I would think that you would do the same for the search, but apparently not because sed complains.
Here is an example program that creates a file called "my_searches". Then it reads each line of this file and performs a search and replace with sed.
#!/bin/bash
When you start the program, you get sed: xregcomp: Invalid content of \{\} for the last line of the file when it is used as the term โsearchโ, but not the term โreplaceโ. I noted lines that give this error with # Does not work above.
------===[ BEGIN ~!@
If you cannot escape the characters in $line (i.e. sed 's/'"$line"'/replaced/' < my_searches ), you will get this error because sed is trying to interpret various characters:
------===[ BEGIN ~!@
So, how do I avoid the search query for sed so that the user can provide any arbitrary text to search for? Or, more precisely, that I can replace the ES= line in my code so that the sed command works for arbitrary text from a file?
I use sed because I am limited to the subset of utilities included in busybox . Although I can use another method (for example, a C program), it would be nice to know if there is a solution to this problem or not.
source share