Home > Net >  How to replace a line in a file containing a $ sign with a value input from user using sed
How to replace a line in a file containing a $ sign with a value input from user using sed

Time:10-08

I have a file with a value for a variable being used which I want to substitute with a value input from the user. I tried the below

sed -i "s/^\$var.*/\$var = \"$uval\" ;/g" file

where uval is the input from the user
example line in file is $var = "value" ;
example value entered by user is unix and the required line in file is $var = "unix" ;
Currently I am getting Unmatched " . Can anyone point as to what I am doing wrong?

CodePudding user response:

Using sed

$ sed -E 's/(\$var = ")[^"]*/\1'"$uval"'/' input_file
$abc = "val" ; $var = "test" ;
  • Related