0

Hi I have following line in shellscript

sed 's_$org_$repl_g' $i > $temp_file

In this $org denotes the name to be change and $repl denotes replacement. I have done echo for both and both are write. $i represent the file name. when I echo below

echo $(sed "s/$org/$repl_g" $i)

Then also it does not replace the word . while when I try this with terminal directly as below

sed 's_Dilip_Agarwal_g' test.txt

then it give correct output by replacing the original one.

Can any body help me where I am wrong in this.

Thanks

agarwal_achhnera
  • 2,582
  • 9
  • 55
  • 84

1 Answers1

2

Do not use command substitution on it. And use double quotes instead of single quotes. Single quotes do not expand parameters. You also have to fix your parameter expansion. _ is also a valid parameter character so you need to use braces to isolate the only characters that would identify the parameters.

sed "s_${org}_${repl}_g" "$i" > "$temp_file"

You can also just use another delimeter that's not a parameter character:

sed "s|$org|$repl|g" "$i" > "$temp_file"
konsolebox
  • 72,135
  • 12
  • 99
  • 105