ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
@xeleema - sorry to be the bearer of bads new but escaping the $ sign is causing only lines with '$b' to be removed. Again the echo addition would yield:
Code:
$ echo './list ./out' | echo sed "/\$b/d"
sed /$b/d
Which I am guessing will still not help OP to remove this line.
Personally I am not sure there is a solution based on this scenario as the escape would actually need to look like the following, assuming no variable being used:
Code:
echo './list ./out' | sed "/\.\/list/d"
So both the dot (.) and the slash (/) need to be escaped to get an accurate removal.
@grail
Hey, but at least I got past the error. In my defense, OP never said what it was they were trying to actually accomplish. :P
So is there any way to feed sed a variable and have it act on it? I'm kinda curious about that myself.
Basically, if the value of b is unknown, how can we write a sed statement that will delete b from the string?
I am guessing, as we still need OP to come back, that the idea initially was to simply use a variable in a sed statement. I think that maybe the unintentional fact that the value of this particular
variable is causing so much angst was not the initial intention.
Personally I am not sure there is a solution based on this scenario as the escape would actually need to look like the following, assuming no variable being used:
Code:
echo './list ./out' | sed "/\.\/list/d"
So both the dot (.) and the slash (/) need to be escaped to get an accurate removal.
Well, there is but it's (surprise) ugly. Generally, for using variables which contain a delimiter
Code:
b="./list"
echo './list ./out' | sed "/${b////\/}/d"
Better: Also escaping the 3. slash. Though not necessary in the above case. However, NOT escaping it will cause trouble when handling the '.'.
So finally
Code:
b="./list"
echo './list ./out' | echo sed "/${b//.\//\.\/}/d"
However, this technique also has it's limitations, e.g., when there are too many characters that would have to be escaped in order to not be interpreted as RegEx.
It might be prettier to escape the search string in the first place:
Code:
export b="\.\/list" # ./list
echo './list ./out'| sed "s/${b}.//"
search and replace by nothing (=effectively removal)
Well, yes, but sometimes you do not know in advance how the variable looks like. And sometimes when you read a text-file you do not want to change it's contents. In those cases escaping the delimiter is a good choice.
I am with rod on this one. Whilst methods have been shown to solve the problem, I can easily see issues arising from both methods.
As usual, the OP will need to explain what there requirement is a bit more as we may be solving issues that are not really there but that the chosen
example made it particularly complicated
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.