Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Put a \ in front of special characters in SED

Shell Programming and Scripting


Tags
perl, perl shift, regex, regular expressions, shift, shift perl

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-15-2007
tugger tugger is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 25 October 2013, 11:22 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Put a \ in front of special characters in SED

I have a file with a text field that could contain characters the sed sees as special characters.

I need to be able to replace this text with other text, however if I use sed like:

sed 's/oldtext/newtext/g'

It errors stating garbled (something or other), which I am putting down to the fact it is reading the text literally and think the likes of / etc should be treated as special chars.

Is there a way around this so I can get it to ignore what it thinks are special chars? (just see the text - Wow *?/ woopee do /$)

By the way thats not the text I want to add Linux
Sponsored Links
    #2  
Old Unix and Linux 10-15-2007
devtakh devtakh is offline Forum Advisor  
Unix Enthusiatic
 
Join Date: Oct 2007
Last Activity: 21 August 2013, 5:20 AM EDT
Location: Bangalore
Posts: 738
Thanks: 0
Thanked 7 Times in 7 Posts
what does ur input look like and what output ur looking for?
Sponsored Links
    #3  
Old Unix and Linux 10-15-2007
Ygor's Unix or Linux Image
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 15 May 2015, 11:55 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
You need to escape the special characters with a backslash, e.g.
Code:
echo 'Wow *?/ woopee do /$' | sed 's/Wow \*?\/ woopee do \/\$/replace/'

If using GNU sed and extended regular expressions with -r, then there are more special characters to escape.
Code:
echo 'Wow *?/ woopee do /$' | sed -r 's/Wow \*\?\/ woopee do \/\$/replace/'

    #4  
Old Unix and Linux 10-16-2007
tugger tugger is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 25 October 2013, 11:22 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Ygor,

The trouble I have is that the text (both new and old) will be held in variables.

I need to somehow interrogate these variables and add a \ before evry special character.

Any ideas.
Sponsored Links
    #5  
Old Unix and Linux 10-16-2007
Ygor's Unix or Linux Image
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 15 May 2015, 11:55 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
Try something like this...
Code:
$ search='Wow *?/ woopee do /$'

$ search=$(echo "$search"|sed 's!\([]\*\$\/&[]\)!\\\1!g')

$ echo "$search"
Wow \*?\/ woopee do \/\$

$ echo 'Wow *?/ woopee do /$'|sed "s/$search/replace/"
replace

Sponsored Links
    #6  
Old Unix and Linux 10-16-2007
tugger tugger is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 25 October 2013, 11:22 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Ygor.

Looks like what I am looking for.

With regard ot the sed statement - 's!\([]\*\$\/&[]\)!\\\1!g'

I know what the s and g are, I am also asumming the ! is the delimeter for the sed command. What is the 1 for? and also the chars within the () are they all the possible special characters within SED? I have searched but I cannot find anything about what are special chars in SED.

Also (sorry) one more thing what are the [] for?
Sponsored Links
    #7  
Old Unix and Linux 10-16-2007
kahuna's Unix or Linux Image
kahuna kahuna is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 12 October 2014, 12:11 PM EDT
Posts: 149
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by tugger View Post
also the chars within the () are they all the possible special characters within SED? I
I'm sure there are more including ^.

If you have perl, perl has a nice feature to deal with special characters.

Code:
perl -pe   "s/\Q$search\E/replace/"

But you might have a problem if your string has a \E or \Q

Last edited by kahuna; 10-16-2007 at 11:45 AM..
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-05-2012 11:40 PM
Special characters sid1982 Shell Programming and Scripting 1 02-19-2010 12:26 AM
sed with special characters itzz.me Shell Programming and Scripting 4 04-06-2009 09:34 PM
get rid of special characters vakharia Mahesh UNIX and Linux Applications 4 07-29-2008 01:36 PM
special characters nawnaw UNIX for Dummies Questions & Answers 2 05-18-2004 03:17 PM



All times are GMT -4. The time now is 09:32 AM.