SED: Replacing $1 with $2 escape problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED: Replacing $1 with $2 escape problem
# 1  
Old 11-27-2008
SED: Replacing $1 with $2 escape problem

Hi all,

I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2

$1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command below doesn't do any escaping which is what I need.

sed "s/$1/$2/g" /tmp/input.txt > /tmp/output.txt

I spent a couple of days making some code which could escape these variables before use in my sed command and it seemed to allow me to enter almost anything but today, when trying to replace one string, with another string that contains an ampersand, some fancy sed backtracking came into play.

function escape {
new=`echo "$1" | sed 's/\\\\/\\\\\\\\/g' | sed 's/\//\\\\\//g' | sed 's/\*/\\\\\*/g' | sed 's/"/\"/g' | sed 's/\"/\\\\\"/g'`
echo $new
}

What I think I really need is a premade escape function for use in sed, or maybe some arguments to sed itself to ignore all special chars and treat them as strings.
# 2  
Old 11-27-2008
Give some examples of lines of your input file, the string to be matched, its replacement and the desired output.

Regards
# 3  
Old 11-27-2008
Hi,

if you want to escape a string, simply use printf with "%q":

Code:
var_a=$(printf "%q" $1)
var_b=$(printf "%q" $2)

Code:
sed "s/${var_a}/${var_b}/g" file

HTH Chris
# 4  
Old 11-28-2008
Thanks for the printf command, I never knew that could do shell escaping for me.

That seems to escape most of the characters that could break my sed, but still if I use an ampersand in my second string, sed blows up on me somehow.

An example I am using to try this out is

Replace "abcd" with "\Bells & Whistles/"

So print f shows "\Bells & Whistles/" as
"\\Bells\ \&\ Whistles/" when var printed to screen
"Bells\ \&\ Whistles/" when var echoed to file

Two problems with this for sed.

1) The special character in sed "&" still seems to be picked up by sed as a special character.

2) The special character in sed "/" doesn't get escaped by printf (should be easy to get around)

Its a pity sed doesn't let me get around the problems I'm having with these special characters. I'm imagining if I manage to fix these special char problems with your help, that down the line I will find more characters that break it....
# 5  
Old 11-28-2008
Hi, I too am new to Unix but discussion my earlier question might be of ome help to you.
View Discussion
# 6  
Old 11-28-2008
Thanks for the link.

I actually fixed the problem with the forward slash. Now its just a matter of fixing the problem with the ampersand being treated as a sed special char.

Here is the code so far

function sed_escape {
new=`echo "$1" | sed 's/\//\\\\\//g'`
echo $new
}
VAR1="unimportant"
VAR2=$(printf "%q" "$1") // does most of the work for me
VAR2=`sed_escape "$VAR2" // takes care of forwardslash character and hopefully with your helpall sed specials like &

sed "s/${VAR1}/${VAR2}/g" // my actual sed command

So just the ampersand is causing me troubles now, im sure other sed specials like ^ will be a problem once the ampersand is fixed.

Last edited by mark007; 11-28-2008 at 07:51 AM..
# 7  
Old 11-28-2008
Oh no, don't do that. You need no function at all. Use

Code:
s#var_a#var_b#g

or every other character you like which you will not likely encouter
in the string instead.

With gnu linux and sed there is no problem at all:

Code:
c="/bells/ & /whistles/"    
d=$(printf "%q" $c)         
sed "s#bells#${d}#" <<< $c

And will give you:

Code:
//bells/ & /whistles// & /whistles/

But as Franklin52 pointed it would be the best to give us some examples
of what you have and what you want.

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to escape % with sed?

Hello, I am running ubuntu 16.04 I searched "how to replace dot by % using sed" but no luck. file My.Name.Is.Earl Expected output My%Name%Is%Earl I tried: sed -i "s|.||g" file sed -i "s|.|{%}|g" file sed -i "s|.|\%|g" file I'd appreciate your help Thank you Boris (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Perl/sed Escape Syntax Problem . . .

Greetings! I've run into this before; and am having a spot of trouble trying to figure out the way that Perl would prefer the following example syntax to be formed:#!/usr/bin/perl use strict; use warnings; use diagnostics; `sed -i 's/Hi Mom!\|Hi Dad!/Bye Everyone!/I' ./test.txt`;Perl... (5 Replies)
Discussion started by: LinQ
5 Replies

3. Shell Programming and Scripting

sed - with escape character

i have string as below str=".<date>" in which i need to replace < with /< , when i tried with sed , got the output. --> echo $str | sed 's/</\\</g' .\<date> when i tried to assign it to a variable , i am not getting the same --> a=`echo $str | sed 's/</\\</g'` ; echo $a... (4 Replies)
Discussion started by: expert
4 Replies

4. Shell Programming and Scripting

Problem with using a sed to add escape character \ before $ and ' symbols

Hi all, I've got a problem with sed. Want to use it to add escape character \ before $ and ' symbols so condition='1'$some will become condition=\'1\'\$some echo "condition='1'$some" | sed 's/\($\)/\\\1/g' is not working properly. Can somebody help me with this please? Regards,... (7 Replies)
Discussion started by: johny_be_good
7 Replies

5. Shell Programming and Scripting

problem in replacing asterisk in sed

Hi all, Sed is the one which always trobules me :( here is my input : *** it industry need to be evolved *** in the world and hope so *** to be dream the output i am expecting is : *** it industry need to be evolved *** in the world and hope so *** to be dream ... (4 Replies)
Discussion started by: panyam
4 Replies

6. Shell Programming and Scripting

problem with sed while replacing a string with another

Hi, I have a line something like this sys,systematic,system I want to replace only the word system with HI I used sed for this as below echo sys,systematic,system | sed 's/system/HI/' but I got output as sys,HIatic,system I wanted output as sys,systematic,HI Please tell me... (9 Replies)
Discussion started by: friendyboy
9 Replies

7. Shell Programming and Scripting

Escape character in sed

Hello experts I am trying to write a shell script which will add ' ' to a unix variable and then pass it to oracle for inserting to a table. I am running the script as root and I have to do a su -c . The problem is the character ' is not recognised inside sed even after adding escape... (1 Reply)
Discussion started by: pvedaa
1 Replies

8. UNIX for Dummies Questions & Answers

sed problem replacing long strings

Hi all, I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2 $1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command... (1 Reply)
Discussion started by: mark007
1 Replies

9. Shell Programming and Scripting

Escape character - sed

Hi All, How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character? Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors :( Example of etc/passwd file: blah:x:1055:600:blah... (6 Replies)
Discussion started by: c00kie88
6 Replies

10. UNIX for Dummies Questions & Answers

possible to escape the \ character in sed?

is it possible to escape the \ character in sed? right now I'm trying to replace all occurances of \ with \\ sed \"s|test|test_replacement|g\" file1 > output; #this works fine sed \"s|\\|\\\|g\" file1 > output; #this generates the following error: sed: -e expression #1, char 17:... (1 Reply)
Discussion started by: gammaman
1 Replies
Login or Register to Ask a Question