[英]sed command is not working to find and replace content in yaml file?
我在 yaml 文件 des.yaml 中有以下内容
simulator:
enabled: false
我想通过将模拟器设为 true 来启用模拟器:
simulator:
enabled: true
通过使用 sed 命令
我尝试使用 sed 命令,但它不起作用:
sed -i 's|simulator\:\n enabled\: false|simulator\:\n enabled\: true|' des.yaml
命令没有抛出任何错误。
请帮忙
使用sed
$ sed -i '/simulator/ {N;s/\(enabled: \).*/\1true/}' input_file
如果找到匹配simulator
,则移至下一行,如果匹配已enabled
,则该值将更改为 true。
输出
simulator:
enabled: true
即使像sed
这样的程序可以解决问题,也应该使用合适的工具,这里: https : //github.com/mikefarah/yq/releases/
yq eval '.simulator.enabled=true' des.yaml
这可能对你有用(GNU sed):
sed -i '/simulator:/{n;/enabled:/s/false/true/}' file
在simulator:
匹配simulator:
,打印该行并获取下一行。
匹配enabled:
,更换false
的true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.