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!

0

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, anubhava. This worked. Thanks for explaining the solution too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.