1

I am trying to replace a substring of a string in a file called db_config using bash.

code in db_config file is

dbname=test_1

I want to replace "test_1" to "production".

The substring can be test_1 or test1 or testing or any random string but the format will be dbname=(whatever the name)

This needs to be changed to dbname=production

I tried both these syntax. Neither worked.

sed -i -e 's/dbname=*/dbname="ihs"/g' db_config

sed -i -e 's/dbname=$/dbname="ihs"/g' db_config

I know how to parse this in python but i cant seem to find a solution in bash.

Thanks in advance!

Harsha Jasti
  • 1,114
  • 1
  • 9
  • 25

1 Answers1

1

You should be using a capture group and .* to match everything after keyname:

sed -i 's/^[[:blank:]]*\(dbname=\).*/\1production/' db_config
  • ^[[:blank:]]* match optional white-spaces before dbname=
  • There is no need of g flag.
anubhava
  • 761,203
  • 64
  • 569
  • 643