0

Below link I have already tried but failed :

find and replace string in a file

It's seems to be very easy solve but I am stuck and not able to resolve

So I have a file inside which there are multiple fields like below :

env : $env

user : abc

passowrd : xyv

tablename : cat_$env 

tablelocation : hdfs://home/ak/cat_$env

So my requirement is to replace $env with $env_details ( which I am generating inside my shell script ) means value of $env_details keep on changing on every run , I have tried below command but got error :

sed -i "s/$env/$env_details/g" "$filename"

Error: sed: -e expression #1 , char 0 no previous regular expression

Could you please help out with my mistake?

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
harsh
  • 17
  • 9
  • You have to escape the variables. See https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern – Chelz Apr 14 '20 at 11:22

1 Answers1

1

Your mistake is that the shell tries to expand $env if it is inside double quotes, just as it expands $filename. Simply write

sed -i 's/$env/$env_details/g' "$filename"

If you want to replace by $env_details contents, keep the double quotes and escape the $ of $env:

sed -i "s/\$env/$env_details/g" "$filename"
Quasímodo
  • 3,812
  • 14
  • 25