harsh
harsh

Reputation: 13

Bash ,Find and Replace a string with a parameterized value generated inside the code?

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?

Upvotes: 0

Views: 57

Answers (1)

Quasímodo
Quasímodo

Reputation: 4004

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"

Upvotes: 1

Related Questions