ghoetker
ghoetker

Reputation: 161

Programmatically modifying a specific line in a text file

Help with what I suspect to be pretty simple would be much appreciated.

I want to be able to programmatically change a certain line in a configuration file. Specifically, the file looks like

...Other lines...
# The name of the database to mount
dbms.active_database=test.db
...Other lines...

I want to be able to replace "test.db" with "production.db", etc. Other programs may add or delete lines, so I can't count on a consistent line number, but there is no other line containing "dbms.active_database="

How might I best do this?

Thank you.

Upvotes: 0

Views: 219

Answers (2)

SLePort
SLePort

Reputation: 15461

With sed:

sed 's/\(^dbms\.active_database=\)test\.db/\1production.db/' file

Upvotes: 1

P....
P....

Reputation: 18391

awk -v name0fNewDb="HEythere.db"  'BEGIN{FS=OFS="="}/dbms.active_database/{$2=name0fNewDb}1'
...Other lines...
# The name of the database to mount
dbms.active_database=HEythere.db
...Other lines...

If I understood problem statement right, this should help you in doing the changes only on the line which consist of "dbms.active_database". Where it will replace the right part of = . name0fNewDb is a variable to which you can assign any text of your requirement.

Upvotes: 1

Related Questions