Home Man
Today's Posts

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replace special characters with Escape characters?

shell scripts

Login to Reply

Thread Tools Search this Thread
# 1  
Old 01-05-2012
Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below.

test!=123-> test\!\=123

!@#$%^&*()-= to be replaced by


Last edited by Scott; 01-05-2012 at 10:01 AM.. Reason: Code tags
# 2  
Old 01-05-2012
echo '!@#$%^&*()-' | sed "s/[!@#$%^&*()-]/\\\&/g"

# 3  
Old 01-05-2012
You listed = to be enclosed by / / too, but in your example it is not. Remove it from the pattern list maybe. Anyway maybe this is what you are looking for:
$> echo 'test!=123'| sed 's_[!@#$%^&*()-=]_\/&\/_g'

# 4  
Old 01-05-2012
echo 'test!=123' | sed 's:[!@#$%^&*()=-]:\\&:g'

# 5  
Old 01-05-2012
And just one more....

export var1='One Way To !@#$%^&*()-= Do It'

echo $var1 | sed 's#\([]\!\(\)\#\%\@\*\$\/&\-\=[]\)#\\\1#g'

One Way To \!\@\#\$\%^\&\*\(\)-\= Do It

# 6  
Old 01-05-2012

Just for education purpose :

The trick is that in between a list [.....] some character have a special meaning :

The hyphen - is usually used to define and interval example : [0-9]

That is the reason why, in order to make it interpreted as litteral , it should be placed at a edge of the list (beginning or end) consider :
$ echo '!@#$%^&*()-' | sed 's/[-!@#$%^&*()]/\\&/g'
$ echo '!@#$%^&*()-' | sed 's/[!@#$%^&*()-]/\\&/g'

There is another special case within a list [......] :
The closing square bracket ]
This special closing square bracket - to be taken as litteral (so that the substitution will apply)- needs to be setup at the very first position in the list otherwise it is take as end of the list or unexpected behaviour may occure.

Consider the following example :
$ echo '!@#[]$%^&*()-' | sed 's/[]!@#[$%^&*()-]/\\&/g'
$ echo '!@#[]$%^&*()-' | sed 's/[-!@#[$%^&*()]]/\\&/g'
$ echo '!@#[]$%^&*()-' | sed 's/[-!@#[]$%^&*()]/\\&/g'

Last edited by ctsgnb; 01-05-2012 at 11:57 AM..
# 7  
Old 01-05-2012
@laknar & off topic
Just wondered why you would want to do this?
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to escape all special characters? AMG1978 Shell Programming and Scripting 2 01-10-2017 09:26 PM
How to escape Special Characters in Expect programming? Mahesh Desai Shell Programming and Scripting 1 11-28-2012 01:42 PM
Escape special characters in SED aakishore Shell Programming and Scripting 2 06-19-2012 04:43 PM
Need help with sed to escape special characters majose Shell Programming and Scripting 4 02-06-2012 04:56 AM
Replace special characters ratheeshjulk Shell Programming and Scripting 0 12-22-2011 08:18 PM
How to replace special characters in vi? rogerben Solaris 2 08-03-2011 05:53 AM
Replace new line with <br /> & escape special characters ted_chou12 Shell Programming and Scripting 2 02-07-2011 08:25 PM
Using sed to replace special characters nimo Shell Programming and Scripting 5 08-12-2010 02:42 AM
awk print $1 escape all special characters unclecameron Shell Programming and Scripting 6 05-29-2010 03:38 PM
Need help to escape special characters in Korn shell script rogers42 UNIX for Dummies Questions & Answers 6 05-14-2009 07:23 AM

All times are GMT -4. The time now is 02:19 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
Show Password