小编Sup*_*pra的帖子

Jq直接替换文件上的文本(如sed -i)

我有一个需要在某种条件下更新的json文件.

示例json

{
   "Actions" : [
      {
         "value" : "1",
         "properties" : {
            "name" : "abc",
            "age" : "2",
            "other ": "test1"
          }
      },
      {
         "value" : "2",
         "properties" : {
            "name" : "def",
            "age" : "3",
            "other" : "test2"
          }
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我正在编写一个脚本,它使用Jq匹配值和更新,如下所示

cat sample.json |  jq '.Actions[] | select (.properties.age == "3") .properties.other = "no-test"'
Run Code Online (Sandbox Code Playgroud)

输出(打印到终端)

{
  "value": "1",
  "properties": {
    "name": "abc",
    "age": "2",
    "other ": "test1"
  }
}
{
  "value": "2",
  "properties": {
    "name": "def", …
Run Code Online (Sandbox Code Playgroud)

bash json edit in-place jq

28
推荐指数
6
解决办法
2万
查看次数

标签 统计

bash ×1

edit ×1

in-place ×1

jq ×1

json ×1