1

I need to replace current value in configuration file with new value which is assigned to variable ,

like

file_name=abc.txt

needs to be replaced like

file_name=xyz.txt

where $file=xyz.txt

I tried

sed -i 's/file_name=.*/file_name=$file/g'  conf_file.conf

however the variable is not getting expanded, it comes like file_name=$file.

any pointers?

jas
  • 10,715
  • 2
  • 30
  • 41
katty
  • 167
  • 1
  • 2
  • 11
  • i tried with above duplicate thread but in my case it shoudl search for 'file_name= ' and then replace with 'file_name=xyz.txt' – katty Sep 28 '18 at 08:33
  • The duplicates say to use double quotes not single quotes. If you tried that it would work. – Jonathan Wakely Sep 28 '18 at 09:28

1 Answers1

3

This should work,assuming that variable file has value:xyz.txt assigned to it:

sed "s/file_name=.*/file_name=${file}/g" file_name

Output:

file_name=xyz.txt

User123
  • 1,498
  • 2
  • 12
  • 26
  • 1
    But this will not work if `$file` has spaces in it. Why have you used `${file}` outside the double quotes? That's not necessary, and makes it wrong if the variable contains whitespace. – Jonathan Wakely Sep 28 '18 at 09:30
  • That's right..Thanks @JonathanWakely for the suggestion..i think it should work with my edited answer.I've removed double quotes `" "` from the `${file}` – User123 Sep 28 '18 at 09:42
  • its not working tried with ......... sed "s/$$file_name=.*/$$file_name=${file}/g" file_name – katty Sep 28 '18 at 10:03
  • 1
    Why have you added `$$`? That will give you the process ID of your current shell, which is not what you want. Why don't you try what people have actually suggested, instead of trying something else that doesn't work? – Jonathan Wakely Sep 28 '18 at 10:07
  • actually in my config file the file_name will be as $$file_name, so tried with that, its informatica config file, where parameters will be given by $$parameter_name – katty Sep 28 '18 at 10:12
  • Please edit your question based on your actual issue, also please let us know how you are defining the variable `file`? – User123 Sep 28 '18 at 10:18