Replace special characters with Escape characters?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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

Code:
\!\@\#\$\%\^\&\*\(\)\-\=


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

# 3  
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:
Code:
$> echo 'test!=123'| sed 's_[!@#$%^&*()-=]_\/&\/_g'
test/!//=/123

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

# 5  
And just one more.... Smilie

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

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

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

# 6  
@Laknar

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 :
Code:
$ 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 :
Code:
$ echo '!@#[]$%^&*()-' | sed 's/[]!@#[$%^&*()-]/\\&/g'
\!\@\#\[\]\$\%\^\&\*\(\)\-
$ echo '!@#[]$%^&*()-' | sed 's/[-!@#[$%^&*()]]/\\&/g'
!@#\[]$%^&*()-
$ echo '!@#[]$%^&*()-' | sed 's/[-!@#[]$%^&*()]/\\&/g'
!@#[]$%^&*()-


Last edited by ctsgnb; 01-05-2012 at 12:57 PM..
# 7  
@laknar & off topic
Just wondered why you would want to do this?
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

2. Shell Programming and Scripting

How to escape Special Characters in Expect programming?

Hi, I have written a unix expect utility "ssh-login.exp" which connects (ssh) to remote host and execute some shell script. I am calling this "ssh-login.exp" utility from another shell script. "ssh-login.exp" takes username, password, host and shell script path to execute on remote host. All... (1 Reply)
Discussion started by: Mahesh Desai
1 Replies

3. Shell Programming and Scripting

How to replace special characters?

Hi Unix Guru, I have an requirement for replace some specail characters in a file, my file came from mainframe. please see below example: when open it with vi 17896660|89059215|04/24/1998 00:00:00.000000| abc 123-453-1312^M<85>^M<85>|124557 if I run cat -v I got following:... (25 Replies)
Discussion started by: ken002
25 Replies

4. Shell Programming and Scripting

Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&amp;MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&amp;MSG_ID= X_EDELIVERY_MESSAGE_ID &amp; BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am... (2 Replies)
Discussion started by: aakishore
2 Replies

5. Shell Programming and Scripting

Need help with sed to escape special characters

Hello Everyone, I need to read an encrypted password from the user and update that value in an xml file. I am trying to use "sed" for searching the appropriate tag and replacing this new value that get from the user. Since the encrypted password can contain special characters(like /,\,&,etc),... (4 Replies)
Discussion started by: majose
4 Replies

6. Shell Programming and Scripting

Replace special characters

I have a line ending with special character and 0 The special character is the field separator for this line in VI mode the file will look like below, but while cat the special character wont display i know the hexa code for the special character ^_ is \x1f and ascii code is \0037, ... (0 Replies)
Discussion started by: ratheeshjulk
0 Replies

7. Solaris

How to replace special characters in vi?

Hi , I want to replace the special characters in the file. For eg: cat abc 1234/4455/acb 234/k/lll/ 234`fs`fd I want to replace / and ` with the letter a and the output should like below. How to achieve this. 1234a4455aacb 234akallla 234afsafd (2 Replies)
Discussion started by: rogerben
2 Replies

8. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 Replies

9. Shell Programming and Scripting

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 Replies

10. UNIX for Dummies Questions & Answers

Need help to escape special characters in Korn shell script

Hi, I would like to display the following message from my shell (Korn) script Copy "old_file.txt" to "new_file.txt" My code looks as follows print "Copy "old_file.txt" to "new_file.txt"" However, when I execute the script, I get the following output Copy old_file.txt to... (6 Replies)
Discussion started by: rogers42
6 Replies

Featured Tech Videos