LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2016, 02:04 PM   #1
rookee
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Rep: Reputation: Disabled
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.
 
Old 08-26-2016, 02:13 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
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.
 
Old 08-26-2016, 09:47 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,535

Rep: Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077Reputation: 2077
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.
 
1 members found this post helpful.
Old 08-27-2016, 06:29 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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
 
1 members found this post helpful.
Old 08-27-2016, 07:20 AM   #5
rookee
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-27-2016, 07:36 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
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.
 
1 members found this post helpful.
Old 08-27-2016, 07:59 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,241

Rep: Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453
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'
 
1 members found this post helpful.
Old 08-27-2016, 08:23 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by rookee View Post
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?
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Escape sequence? coalDik Linux - Newbie 1 12-18-2014 02:38 PM
Screen escape sequence djkadu Linux - General 4 10-03-2014 11:16 AM
[SOLVED] unknown escape sequence Shao Lung Programming 2 04-28-2011 11:38 PM
unknown escape sequence: '\040' jiikka Linux - General 1 03-09-2009 07:48 AM
escape sequence help in C name_in_use450 Linux - General 6 07-01-2004 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration