rahul
rahul

Reputation: 304

sed to replace strings with special characters

I'm trying to replace particular strings from a text file

Input: Computer Peripherals Internet Technology C/C++

Output : Shopping Generic Programming

When i try using the follwing sed:

refcat="Computer Peripherals"
cat1="Shopping"
echo $list | sed -e 's@$refcat@$cat1@g'

I get the following result:

Computer Peripherals Internet Technology

I get the output on a different line. Can someone pls help me with this?? My Input and Output values can have special characters in them ( Except '|' or '@' )

Upvotes: 0

Views: 339

Answers (2)

CS Pei
CS Pei

Reputation: 11047

For the shell to expand variables, you need double quotes I think

echo $list | sed -e "s@$refcat@$cat1@g"

my example

echo Computer Peripherals Internet Technology C/C++ | sed -e "s@${refcat}@${cat1}@g"

result

Shopping Internet Technology C/C++

Upvotes: 1

Adam Spiers
Adam Spiers

Reputation: 17916

Single quotes don't interpolate shell variables, so change to double quotes:

echo $list | sed -e "s@$refcat@$cat1@g"

Upvotes: 1

Related Questions