Substitution when special charcters involved


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Substitution when special charcters involved
# 1  
Old 11-13-2008
Substitution when special charcters involved

I am trying to substitute a substring in a file and am having difficulty due to the presence of 'special characters'

I tried

sed -e "s/Bob's birthday 13/11/08 (today)/Bob's birthday 14/11/08 (tomorrow)/" file1

This does not action any change due to the square brackets.

How can I cater for special characters when making a substitution such as this?

Your help is much appreciated.
# 2  
Old 11-13-2008
Escape the special characters with a backslash:

Code:
echo "Bob's birthday 13/11/08 (today)"|\
sed 's/13\/11\/08 (today)/14\/11\/08 (tomorrow)/'
Bob's birthday 14/11/08 (tomorrow)

# 3  
Old 11-13-2008
Thanks for the above, however what if both strings are variables (i.e $string1 and $string2).
# 4  
Old 11-13-2008
Then you will have to do it in the variables contents. You could write a library function (make_regexp()) out of it, which gets a string and escapes it properly:

Code:
function pMakeRegexp
{
if [ $# -ne 1 ] ; then
     return 1
fi

# add to the characters in the bracket if i have overlooked one
print - "$1" | sed 's/[/\&^*.$]/\\&/g'

return 0
}

# main
typeset var="some \ special ^ characters / are & here"
typeset regex_var="$(pMakeRegexp "$var")"

print - "var is \"$var\" // regexed var is \"$regex_var\""
print - "xx $var xx" | sed "s/$regex_var/=&=/"

exit 0

As you can see the regexed version "$regex_var" matches normal version "$var" and the replacement (adding the equal signs before and after) is taking place. Similarily you can put the replacement string through pMakeRegexp() too.

I hope this helps.

bakunin
# 5  
Old 11-13-2008
Or use them like this:

Code:
VAR1="13\/11\/08 (today)"
VAR2="14\/11\/08 (tomorrow)"

echo "Bob's birthday 13/11/08 (today)"|\
sed "s/${VAR1}/${VAR2}/"
Bob's birthday 14/11/08 (tomorrow)

# 6  
Old 11-14-2008
With sed you can use any character as the pattern separator, e.g. you can replace / with |
Code:
sed -e "s|Bob's birthday 13/11/08 (today)|Bob's birthday 14/11/08 (tomorrow)|" file1

See e.g.
https://www.unix.com/shell-programmin...#post302244111
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk statement to grep (bit involved)

Hellow: I have the following data. id1 xxx xxx xxx id1 xxxx xxx xxx id2 xxx xxx xxx id2 xxxx xxx xxx id2 In my awk script which reads the file containing the above data I have the following code: myline=<inputdata> which is shown above What I am trying to find out is how may... (5 Replies)
Discussion started by: wincrazy
5 Replies

2. Homework & Coursework Questions

Meta charcters

find out lines in a given file consisting of the following pattern BCAA, BCAAA, BCAAAA, BCAAAAA, BCAAAAAA (1 Reply)
Discussion started by: Phaneendra G
1 Replies

3. UNIX for Dummies Questions & Answers

Meta charcters

Find out lines in a given file consisting of the following pattern BCAA, BCAAA, BCAAAA, BCAAAAA, BCAAAAAA (0 Replies)
Discussion started by: Phaneendra G
0 Replies

4. Solaris

nestat on server involved in high traffic network

Hi All My Server is doing a very intense netowrk traffic operations and the cards are under very high pressure. I need to call NETSTAT on the shell. Do you know whether this command, under high pressure, might have some impact on the server traffic or can I proceed without any problem? (2 Replies)
Discussion started by: manustone
2 Replies

5. Shell Programming and Scripting

cat in linux, file holding special charcters

Hi I'd like to cat, in linux, a file that holds special charcters, like "-->" and ">" and "]" For example I have a file named test123.txt it looks like this: 2008-09-11 00:27:01,496 - < 0 > --> Start calculation of pattern , Pattern was split to pattern graphs < 0 > System Tqls Optimizer... (5 Replies)
Discussion started by: liav
5 Replies

6. UNIX for Advanced & Expert Users

Line Longer Than 2048 Charcters

I have a csv file with a record size of greater than 2048.So when i try to open the file in VI..This is the error i get (test.csv" A line cannot be longer than 2048 characters) Is there a way i can change this parameter to read a bigger line (2 Replies)
Discussion started by: kris01752
2 Replies

7. UNIX for Advanced & Expert Users

remove charcters

How do i remove single quotes(') from a file. Can we use sed for it (2 Replies)
Discussion started by: kris01752
2 Replies

8. Solaris

Handling Special Charcters

Dear All, I have created a UTF-8 database to store multi-lingual charcters. Below is the query from which i insert from Winsql (front-end third party database browser tool), the data gets inserted properly. insert into a (no, lbl) values (1, "Cliquez ici pour revenir Ã_ la recherche de... (2 Replies)
Discussion started by: lloydnwo
2 Replies

9. HP-UX

How to check patches involved

Hi I need to check if the following patches are installed in a HP-UX machine "GOLDQPK11i, which in turn includes both GOLDAPPS11i and GLODBASE11i How can I go about doing it, am still a struggling sys admin! Saw this command showrev -p but command not found in the machine thou! Thanks... (3 Replies)
Discussion started by: gelbvonn
3 Replies

10. UNIX for Dummies Questions & Answers

Help! a free Sprite is involved!

Can anyone define the following for me? /etc/rc.c/init.d/iptables restart Our resident "Geek" is giving away a prize if I can tell him what this means. (5 Replies)
Discussion started by: txyzzy
5 Replies
Login or Register to Ask a Question