0
votes

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 '@' )

2

2 Answers

1
votes

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

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

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++