在配置文件中替换Line

时间:2012-01-29 13:31:29

标签: regex replace sed

我想自动替换Redmine配置文件的行。配置文件如下所示:

# MySQL (default setup).
....
production:
  adapter: mysql
  database: redmine
  host: localhost
  username: root
  password:
  encoding: utf8

development:
  adapter: mysql
  database: redmine_development
  host: localhost
  username: root
  password:
  encoding: utf8
  ....

我只想在生产部分之后替换密码,数据库等......

如何使用sed

执行此操作

1 个答案:

答案 0 :(得分:2)

基于您的示例文件:

sed -r '/^production:/,/^$/{s/(\s*database:).*/\1newDB/;
        s/(\s*password:).*/\1newPWD/}' yourConfig

上面的cmd会将“生产”部分中的“数据库”和“密码”设置更改为newDB和newPWD。

输出:

...
production:
  adapter: mysql
  database:newDB
  host: localhost
  username: root
  password:newPWD
  encoding: utf8

development:
  adapter: mysql
  database: redmine_development
  host: localhost
  username: root
  password:
  encoding: utf8
...