Replace value of a variable in a file through script using sed

Hi,

I am trying to replace the value of a variable in a file through another script.

Example:
Filename : abc.txt contents:
a=10
b=20
c=30

Need to change the value of, say, b - Tried using the following:

sed "s/${b}/15/g" abc.txt

Have tried various forms of sed (with single quotes, without quotes, with -e option, etc) but getting the same error with all:

sed: -e expression #1, char 0: no previous regular expression

:wall:

Thanks for any help in advance :slight_smile:

You are getting that error because the shell variable, ${b} , is unset or null. After its expansion, the sed command becomes s//15/g . In sed, an empty regular expression is equivalent to the most recently used regular expression. Since there is none, you get that error.

Regards,
Alister

Thanks Alister...

Is there any way this can be overcome - so that I can have variable in the file referenced directly

how about:

sed "s/^b=.*/b=${b}/" abc.txt
1 Like

It worked - Thanks Chubler_XL :b: