Unix/Linux Go Back    


Shell Programming and Scripting 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 Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
laknar's Unix or Linux Image
laknar laknar is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 10 November 2016, 8:28 AM EST
Posts: 85
Thanks: 3
Thanked 0 Times in 0 Posts
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 10:01 AM.. Reason: Code tags
Sponsored Links
    #2  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 20 June 2018, 4:36 AM EDT
Posts: 9,061
Thanks: 400
Thanked 1,279 Times in 1,082 Posts


Code:
echo '!@#$%^&*()-' | sed "s/[!@#$%^&*()-]/\\\&/g"
\!\@\#\$\%\^\&\*\(\)\-

Sponsored Links
    #3  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 13 June 2018, 5:49 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,574
Thanks: 178
Thanked 569 Times in 483 Posts
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  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 20 June 2018, 5:21 PM EDT
Location: Foxborough, MA
Posts: 8,296
Thanks: 248
Thanked 830 Times in 775 Posts


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

Sponsored Links
    #5  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
in2nix4life's Unix or Linux Image
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 9 October 2016, 12:41 PM EDT
Location: East Coast
Posts: 621
Thanks: 1
Thanked 177 Times in 163 Posts
And just one more.... Linux



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

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

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

Sponsored Links
    #6  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
ctsgnb's Unix or Linux Image
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 3 April 2018, 6:42 AM EDT
Location: France
Posts: 2,967
Thanks: 88
Thanked 640 Times in 610 Posts
@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 11:57 AM..
Sponsored Links
    #7  
Old Unix and Linux 01-05-2012   -   Original Discussion by laknar
methyl's Unix or Linux Image
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
@laknar & off topic
Just wondered why you would want to do this?
Sponsored Links
Closed

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 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
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 10:29 PM.