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.
Keep going with the escapes or was there a question I am missing here? Ultimately you are saying there is a chance of another lines being almost identical hence you need to put the entire line in. If this
is the case, then you have no other choice but to escape all the back slashes and perhaps even the period.
You can play games with the "s" command, which can use any character as the delimiter, and the "T" command, which will branch if no substitution was done:
Code:
sed -i "s@/utap:2345:respawn:/usr/local/guardium/guard_stap/guard_stap/@anything@;T;d" test1
The "s" command will replace the matched string with "anything", the "T" command (with no argument) will branch to the end of the script if no substitution was done, otherwise (i.e., if there was a match) the "d" command deletes the line entirely.
However, you still have to watch out for any regex wildcards in the string. Those might cause a false match (e.g., ".") or prevent a match (e.g., looking for a literal "*"). You still have to escape any of those.
You might find "grep -F -v" to be a better choice, though there's no "in-place" option there.
Please excuse me for poorly wording my question earlier. Let me rephrase it. What I'm trying to do is, say to the system that "Hey system, take this text utap:2345:respawn:/usr/local/guardium/guard_stap/guard_stap/and remove the special meaning of the special characters which in this case is mostly /, consider it just as regular text and process it. But I want to do that in the sed command like below".
Code:
sed -i "\(utap:2345:respawn:/usr/local/guardium/guard_stap/guard_stap)/d" test1
Basically I'm trying to remove an entire line when utap:2345:respawn:/usr/local/guardium/guard_stap/guard_stap matches with one of it's lines/part of a line from the file "test1" through a script
I think we all pretty much hit that nail and as I said, if there are possibly more lines containing the same string in almost its entirety then you will need to put in the entire line and if using the '/d'
option then you will have to escape all the slashes. As pointed out by rknichols an alternative would be you could leave the line empty instead of deleting it with something like:
Code:
sed -i 's@utap:2345:respawn:/usr/local/guardium/guard_stap/guard_stap@@' file
If you are looking for ways to not type in the whole line, maybe you can consider if the line to be deleted is anchored, so then you could do something like:
Code:
sed -i '/^utap:2345:respawn:.*guard_stap/d' file
So here if your line to be removed start with 'utap...' the carat helps tell sed this.
in sed you can use different delimiters as it was described in post #3, that is a bit tricky, but was explained well (at least for me).
From the other hand there is another trick I used to "employ": just write dot instead of special chars, so:
Code:
sed '/utap:2345:respawn:.usr.local.guardium.guard_stap.guard_stap/d'
will work perfectly. Unfortunately sed cannot handle that string "in one", you either need to use the solution in post #3 or preprocess that string.
Perl has very nice functions to escape special chars in one
Please excuse me for poorly wording my question earlier...
We try to help you while suggesting what we consider a correct solution to the exposed problem.
In your source file (inittab ?), there are more than one line containing ' */guard_stap '? If yes, you want to keep some of these lines or delete all of them?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.