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?

2
  • i tried with above duplicate thread but in my case it shoudl search for 'file_name= ' and then replace with 'file_name=xyz.txt' Commented Sep 28, 2018 at 8:33
  • The duplicates say to use double quotes not single quotes. If you tried that it would work. Commented Sep 28, 2018 at 9:28

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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}
its not working tried with ......... sed "s/$$file_name=.*/$$file_name=${file}/g" file_name
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?
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.