-1

I have a string in my .sh file

  Path="etc/name"

This I need to write into a file.

This is my File.txt

 Name:Tom

 My_Path:add path here 

 Address:Xyz

Here in this file I need to replace path by the 'path' variable in shell script.

I used

   sed -i -e "s/\(My_Path=\).*/\1$path/" file.txt

But getting error like :

   Sed: -e expression #1, unknown option to s

my desired output is , the file My_Path.txt should contain

 Name:Tom

 My_Path:"etc/name" 

 Address:Xyz
Aravind
  • 41
  • 5

2 Answers2

0

First escape slashes on your variable

path=$(echo $path | sed "s/\//\\\\\//g")

Then use this sed line

sed -i "s/My_Path:.*$/My_Path:$path/" file.txt
Francesco Gasparetto
  • 1,819
  • 16
  • 20
0

The issue is your Path variable also has / (forward-slash) which gets interpreted as delimiter of your sed.

So try using some other delimiter. Here i am using | as delimiter:

sed -i -e "s|\(My_Path:\).*|\1$Path|" file.txt

Also refer: what-delimiters-can-you-use-in-sed

Fazlin
  • 2,285
  • 17
  • 29