LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Escape sequence (https://www.linuxquestions.org/questions/programming-9/escape-sequence-4175588057/)

rookee 08-26-2016 02:04 PM

Escape sequence
 
HI All,

I'm trying to remove the special meaning of the forward slash (/) in the below all at once
"
Code:

utap:2345:respawn:/usr/local/guardium/guard_stap/guard_stap /usr/local/guardium/guard_stap/guard_tap.ini
" Can some help me understand how I achieve that. Thanks in advance.

Code:

sed -i "/utap:2345:respawn:\/usr\/local\/guardium\/guard_stap\/guard_stap/d" test1
This works but I'm looking for something to use for the whole text at once.

grail 08-26-2016 02:13 PM

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.

rknichols 08-26-2016 09:47 PM

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.

keefaz 08-27-2016 06:29 AM

Depending on the source file, you could match fewer things.
Eg if a line containing "guard_stap" is unique
Code:

sed -i '/guard_stap/d' test1

rookee 08-27-2016 07:20 AM

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

grail 08-27-2016 07:36 AM

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.

pan64 08-27-2016 07:59 AM

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
Code:

check:
/\Q<anything>\E/

, but you can try something like this in sed too:
Code:

sed 's/\([/*?]\)/\\\1/g'

keefaz 08-27-2016 08:23 AM

Quote:

Originally Posted by rookee (Post 5596858)
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?


All times are GMT -5. The time now is 12:01 PM.