linux - 使用sed修改配置文件

标签 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 脚本执行此操作?

最佳答案

你可以使用

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

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

  • ^ - 字符串的开始
  • \( *bindIp *: *\) - 捕获第 1 组(用 RHS 中的 \1 表示):零个或多个空格, bindIp,零个或多个空格,: 和零个或多个空格
  • .* - 行的其余部分

关于linux - 使用sed修改配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271511/

相关文章:

linux - 遍历目录中特定扩展名的所有文件

linux - 如何在 bash shell 中格式化字符串?

linux - 如何在 Ubuntu 中使用 Tab 键完成 screen -r ?

python - 什么是 python 'requests' api 等效于我的 shell 脚本 curl 命令?

mongodb - Spring Data MongoDB:@TextIndexed索引已经存在不同的选项

Bash 脚本 : Find minimum value in a script

linux - libusb1 do_configure 任务失败,错误为 "udev support requested but libudev not installed"

linux - 无法使用 vsftpd centos 7 通过 ftp 写入/编辑?

javascript - 按字母顺序排序结果列表 : with MongoDB or JavaScript client?

mongodb - 如果我的 MongoDB 中禁用了脚本,我该如何测试