使用sed修改配置文件

时间:2018-09-11 08:12:19

标签: linux mongodb bash sed

mongod.conf文件

 # mongod.conf
 # for documentation of all options, see:
 # http://docs.mongodb.org/manual/reference/configuration-options/
 # Where and how to store data.
   storage:
   dbPath: /var/lib/mongodb
   journal:
   enabled: true
 #  engine:
 #  mmapv1:
 #  wiredTiger:
 # where to write logging data.
   systemLog:
   destination: file
   logAppend: true
   path: /var/log/mongodb/mongod.log
 # network interfaces
   net:
   port: 27017
   bindIp: 127.0.0.1
 #processManagement:
 #security:
 #operationProfiling:
 #replication:
 #sharding:
 ## Enterprise-Only Options  
 #auditLog: 
 #snmp:

这是我拥有的mongoDB配置文件,我需要像这样更新文件中的密钥,

port: 27017 
bindIp: 0.0.0.0 
#security: 
keyFile:/opt/mongodb/keyfile 
authorization: enabled 
#replication: 
replSetName: mongoreplica1

如何使用bash脚本执行此操作?

1 个答案:

答案 0 :(得分:1)

您可以使用

来修复sed命令
sed -i 's/^\( *bindIp *: *\).*/\10.0.0.0/'

BRE POSIX模式^\( *bindIp *: *\).*匹配

  • ^-字符串的开头
  • \( *bindIp *: *\)-捕获组#1(在RHS中称为\1):零个或多个空格,bindIp,零个或多个空格,:和零或更多空格
  • .*-该行的其余部分