使用 JQ 后就地编辑文件 Select
JQ edit file in place after using JQ Select
在 sed 中使用 JQ 就地编辑 json 文件,例如 -i
我找到了很多解决方案,例如
jq ... input.json > tmp.json && mv tmp.json input.json
这可行,但我需要过滤我的文件,添加一些数据,然后将其放回原处。 (即更新文件中的特定对象)
我的文件包含 1000 多个具有 不同 Element
的对象。我将始终在 Element
上使用过滤器,我缩短了问题。我有一个示例文件 original.json
...
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "optouts",
"Path": "/optouts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "subscriptionTypes",
"Path": "/subscription-types",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
我想按 objectName
过滤 contacts
添加一些数据到空 IBM 字段并保存到文件
应用相同的逻辑将包含 "objectName": "contacts"
的对象的 IBM 字段就地编辑为 "Y"
jq 'select(.objectName == "contacts") | .IBM = "Y"' original.json > tmpjson.json && mv tmpjson.json original.json
现在我的文件 original.json
显示
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "Y",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
预期结果
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "Y",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "optouts",
"Path": "/optouts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "subscriptionTypes",
"Path": "/subscription-types",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
使用select后好像不能再使用提供的解决方案https://github.com/stedolan/jq/wiki/FAQ#general-questions
您的 jq 过滤器不是您需要的过滤器,因为 select
选择。即过滤掉不符合选择条件的对象。
这个 jq 过滤器应该做你想做的事:
if (.objectName == "contacts") then .IBM = "Y" else . end
至于覆盖文件,许多有权访问 sponge
(CLI 实用程序 moreutils 集合的一部分)的人都在使用它,例如
jq 'if (.objectName == "contacts") then .IBM = "Y" else . end' input.json |
sponge input.json
在 sed 中使用 JQ 就地编辑 json 文件,例如 -i
我找到了很多解决方案,例如
jq ... input.json > tmp.json && mv tmp.json input.json
这可行,但我需要过滤我的文件,添加一些数据,然后将其放回原处。 (即更新文件中的特定对象)
我的文件包含 1000 多个具有 不同 Element
的对象。我将始终在 Element
上使用过滤器,我缩短了问题。我有一个示例文件 original.json
...
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "optouts",
"Path": "/optouts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "subscriptionTypes",
"Path": "/subscription-types",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
我想按 objectName
过滤 contacts
添加一些数据到空 IBM 字段并保存到文件
应用相同的逻辑将包含 "objectName": "contacts"
"Y"
jq 'select(.objectName == "contacts") | .IBM = "Y"' original.json > tmpjson.json && mv tmpjson.json original.json
现在我的文件 original.json
显示
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "Y",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
预期结果
{
"Element": "acton",
"objectName": "contacts",
"Path": "/contacts",
"Scenario": "",
"serviceLevel": "",
"IBM": "Y",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "optouts",
"Path": "/optouts",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
{
"Element": "acton",
"objectName": "subscriptionTypes",
"Path": "/subscription-types",
"Scenario": "",
"serviceLevel": "",
"IBM": "",
"Dependency": "",
"Gap": "",
"clientPhase": "",
"parentPhase": "",
"existsToday": ""
}
使用select后好像不能再使用提供的解决方案https://github.com/stedolan/jq/wiki/FAQ#general-questions
您的 jq 过滤器不是您需要的过滤器,因为 select
选择。即过滤掉不符合选择条件的对象。
这个 jq 过滤器应该做你想做的事:
if (.objectName == "contacts") then .IBM = "Y" else . end
至于覆盖文件,许多有权访问 sponge
(CLI 实用程序 moreutils 集合的一部分)的人都在使用它,例如
jq 'if (.objectName == "contacts") then .IBM = "Y" else . end' input.json |
sponge input.json