使用 jq 就地修改 json 中的键值 [英] Modify a key-value in a json using jq in-place

查看:123
本文介绍了使用 jq 就地修改 json 中的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 json,我想在其中修改特定值,但终端总是显示带有修改值的 json,但它实际上并未更改特定文件中的值.示例 json:

I have a json in which I want to modify a particular value but the terminal always displays the json with the modified value but it does not actually change the value in the particular file. Sample json:

{
   name: 'abcd',
   age: 30,
   address: 'abc'
}

我想更改文件本身中的地址值,但到目前为止我一直无法这样做.我尝试使用:

I want to change the value of address in the file itself but so far I've been unable to do so. I tried using:

jq '.address = "abcde"' test.json

但是没有用.有什么建议吗?

but it didn't work. Any suggestions?

推荐答案

AFAIK jq 不支持就地编辑,所以你必须先重定向到一个临时文件,然后用它,或使用 moreutils 包中的 sponge 实用程序,如下所示:

AFAIK jq does not support in-place editing, so you must redirect to a temporary file first and then replace your original file with it, or use sponge utility from the moreutils package, like that:

jq '.address = "abcde"' test.json|sponge test.json

还有其他技术可以重定向到同一文件",例如将输出保存在变量 e.t.c 中.如果您想了解更多信息,Unix & Linux StackExchange"是一个不错的起点.

There are other techniques to "redirect to the same file", like saving your output in a variable e.t.c. "Unix & Linux StackExchange" is a good place to start, if you want to learn more about this.

这篇关于使用 jq 就地修改 json 中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆