我有一個meta.json包含以下內容的 Json 檔案,我想使用jq filter在下面的 json 檔案中進行查找和附加操作。例如下面的 json 值name就是demo這樣,將一個字串附加到它上面,-test這樣最終值將是demo-test
{
"version": "2.2.0",
"vname": "tf",
"data": {
"name": "demo",
"udn": {
"description": "The `main` tf in this template creates a resource`. "
}
}
}
更新后的 json 檔案應包含如下資料
{
"version": "2.2.0",
"vname": "tf",
"data": {
"name": "demo-test",
"udn": {
"description": "The `main` tf in this template creates a resource`. "
}
}
}
JQ過濾器是否支持這個?我們怎么能做到這一點?
uj5u.com熱心網友回復:
使用jq
$ jq '.data.name |= . "-test"' meta.json
{
"version": "2.2.0",
"vname": "tf",
"data": {
"name": "demo-test",
"udn": {
"description": "The `main` tf in this template creates a resource`. "
}
}
}
使用sed
$ sed '/\<name\>/s/[[:punct:]]\ $/-test&/' meta.json
{
"version": "2.2.0",
"vname": "tf",
"data": {
"name": "demo-test",
"udn": {
"description": "The `main` tf in this template creates a resource`. "
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/473099.html
