Computing.Net > Forums > Unix > parameters in sed

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

parameters in sed

Reply to Message Icon

Name: kris92
Date: May 25, 2005 at 23:54:43 Pacific
OS: UNIX
CPU/Ram: XEON
Comment:

Hi
I am using sed in a shell script. I am having problems in using parameters in sed for search and replace.

TEST=`grep "'/" source | head -1`
sed s/$TEST/string1/g source.txt > target.txt
This doesnt work but when i define the variable TEST as
TEST="string2"
sed s/$TEST/string1/g source.txt > target.txt

it works. can anyone please help



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: May 26, 2005 at 05:46:11 Pacific
Reply:

Two things are messing up sed. After $TEST is evaluated, the single quote(s) that it brings in is now treated as a shell delimiter. To get around that, place your sed command within double-quotes. Secondly, $TEST is also bringing in a slash, and that messes up the sed command delineation. To get around that, switch the sed delimiter character to something other than slash that you know will not be in your $TEST or string1 fields.

With $TEST containing any number of single quotes and slashes, the following works for me:

sed "s!$TEST!string1!g"


0

Response Number 2
Name: kris92
Date: May 27, 2005 at 00:19:43 Pacific
Reply:

thanks a lot .... it worked for me too


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: parameters in sed

using parameters in sed??? www.computing.net/answers/unix/using-parameters-in-sed/5783.html

parameters to sed doesnt work www.computing.net/answers/unix/parameters-to-sed-doesnt-work/7467.html

How to use if/else logic in sed/awk www.computing.net/answers/unix/how-to-use-ifelse-logic-in-sedawk/6207.html