linux - Bash:将对象添加到 json 文件

标签 linux bash shell

我知道如何回显/添加文本到文件末尾:

echo "{ "fruit":"apple" , "amount":"10" }" >> file.txt

我的问题是如何将对象添加到下面的 json 文件中:

文件 - file.txt(空):

{
"fruit": [

]
}

预期结果:

{
"fruit": [
{ "fruit":"apple" , "amount":"10" } #object to add

]
}

最佳答案

ed 是标准的文本编辑器。

#!/bin/bash

{
ed -s file.json <<EOF
/\"fruit\": \[
a
{ "fruit":"apple" , "amount":"10" } #object to add
.
wq
EOF
} &> /dev/null

虽然不知道为什么要为此使用 bash,但周围有更好的工具!

完成。

关于linux - Bash:将对象添加到 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334238/

相关文章:

git - 用于 Minecraft 服务器备份/版本控制的 Git 替代品

bash - 通过传递参数来捕获函数?

linux - 用于查找两个值之间差异的 Shell 脚本

linux - 当 linux 内核崩溃时,内核堆栈和调用跟踪如何记录到控制台?

linux - 剪切并tr : command not found

linux - 在 linux shell 中确定当前打开的 URL 的最佳方法

regex - Bash 正则表达式匹配 "0xffffffc0006e0584 is in some_function (/path/to/my/file.c:93)."

linux - Flyway 连接到 Linux 上的数据库集群

c++ - 克服下载 CSV 文件进行解析时的问题。 Linux 与 Windows 换行符

linux - 多行写入失败(权限被拒绝)