簡體   English   中英

sed 命令無法在 yaml 文件中查找和替換內容?

[英]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: ,更換falsetrue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM