user1929905
user1929905

Reputation: 457

sed command to take variable

I have file File1.txt in which i have to replace a text using sed command

File1.txt contents

EURAMOUNTTOBEREPLACED

I have a AIX shell script for replacing the text AMOUNTTOBEREPLACED

Contents of the shell script

sum=27
sed 's/AMOUNTTOBEREPLACED/"$sum"/g'  File1.txt >> temp
mv temp FileNew.txt

After executing the script, the contents of FileNew.txt is as below

EUR"$sum"

But Expected output should be

EUR27

Please help how to do?

Upvotes: 2

Views: 1777

Answers (2)

bagavadhar
bagavadhar

Reputation: 129

I think the one you want is like this:

sed 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt >> temp

Basically single qouting takes the string to sed and skips shell which is wrong. You want the shell to interpret the variable so thats what i did. And further if you happen to have gnu version of sed. Then you can do

sed -i 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt

which compressed these two statement in your code to one in above:

sed 's/AMOUNTTOBEREPLACED/'$sum'/g'  File1.txt >> temp
mv temp FileNew.txt

Upvotes: 1

Vijay
Vijay

Reputation: 67231

use the below sed command instead:

sed -e "s/AMOUNTTOBEREPLACED/\"${sum}\"/g"

Upvotes: 0

Related Questions