|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ![]() |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
what does ur input look like and what output ur looking for?
|
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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 | ||
|
![]() |
| Tags |
| perl, perl shift, regex, regular expressions, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | |
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 |
|
|