[英]SED/AWK how to replace a value if a parent key matches in json file
如果父項是 refreshinterval,我正在嘗試替換默認值。我嘗試使用 jq 和 sed 但無法獲得我預期的 output。有人可以幫助獲得預期的答案。
{ "doubleClickDiff": {
"type": "boolean",
"title": "Show diff on double click",
"description": "If true, doubling clicking a file in the list of changed files will open a diff.",
"default": false
},
"historyCount": {
"type": "integer",
"title": "History count",
"description": "Number of (most recent) commits shown in the history log",
"default": 25
},
"refreshInterval": {
"type": "integer",
"title": "Refresh interval",
"description": "Number of milliseconds between polling the file system for changes.",
"default": 10000
},
"simpleStaging": {
"type": "boolean",
"title": "Simple staging flag",
"description": "If true, use a simplified concept of staging. Only files with changes are shown (instead of showing staged/changed/untracked), and all files with changes will be automatically staged",
"default": false
}}
我的 output 應該僅在鍵為 refreshInterval 時才替換默認值
"refreshInterval": {
"type": "integer",
"title": "Refresh interval",
"description": "Number of milliseconds between polling the file system for changes.",
"default": 40000
},
jq '."refreshInterval"."default"=40000' plugin.json
sed -i 's/\"default\":.*/\"default\": '40000'/g' "demo.json"
您發布的jq
命令應該可以正常工作,但它不會更新文件,它只會輸出更新后的結果。 您可以存儲該 output 並用它覆蓋原始文件,如下所示:
jq '.refreshInterval.default = 40000' plugin.json > jq_output
mv jq_output plugin.json
注意不要嘗試將其簡化為jq '.refreshInterval.default = 40000' plugin.json > plugin.json
因為將首先處理重定向並在jq
讀取文件之前擦除文件的內容。 如果你想避免使用臨時文件,你可以使用一個實用程序,例如moreutils
package 中的sponge
,它允許就地編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.