使用'jq'JSON shell工具进行搜索和替换

时间:2015-10-30 16:45:06

标签: mongodb search replace sed jq

这里从JSON输出中提取。 JSON文档automationConfig是MongoDB Ops Manager,是管理mongod的商业产品。

$ cat automationConfig |  jq '.processes[]'
{
  "args2_6": {
    "net": {
      "port": 28200
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-cache-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-cache-dbspace/wooomtest-cache-dbspace_0",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-cache-dbspace/wooomtest-cache-dbspace_0/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-ca-sc-wooom-stack4-prd-1.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-cache-dbspace_0",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28200
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-cache-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-cache-dbspace/wooomtest-cache-dbspace_1",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-cache-dbspace/wooomtest-cache-dbspace_1/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-ca-sc-wooom-stack4-prd-2.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-cache-dbspace_1",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28200
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-cache-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-cache-dbspace/wooomtest-cache-dbspace_2",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-cache-dbspace/wooomtest-cache-dbspace_2/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-ca-sc-wooom-stack4-prd-3.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-cache-dbspace_2",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28000
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-identity-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-identity-dbspace/wooomtest-identity-dbspace_3",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-identity-dbspace/wooomtest-identity-dbspace_3/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-id-sc-wooom-stack4-prd-1.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-identity-dbspace_3",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28000
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-identity-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-identity-dbspace/wooomtest-identity-dbspace_4",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-identity-dbspace/wooomtest-identity-dbspace_4/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-id-sc-wooom-stack4-prd-2.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-identity-dbspace_4",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28000
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-identity-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-identity-dbspace/wooomtest-identity-dbspace_5",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-identity-dbspace/wooomtest-identity-dbspace_5/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-id-sc-wooom-stack4-prd-3.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-identity-dbspace_5",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28300
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-library-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-library-dbspace/wooomtest-library-dbspace_6",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-library-dbspace/wooomtest-library-dbspace_6/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-lib-sc-wooom-stack4-prd-1.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-library-dbspace_6",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28300
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-library-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-library-dbspace/wooomtest-library-dbspace_7",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-library-dbspace/wooomtest-library-dbspace_7/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-lib-sc-wooom-stack4-prd-2.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-library-dbspace_7",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28300
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-library-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-library-dbspace/wooomtest-library-dbspace_8",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-library-dbspace/wooomtest-library-dbspace_8/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-lib-sc-wooom-stack4-prd-3.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-library-dbspace_8",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28100
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-storage-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-storage-dbspace/wooomtest-storage-dbspace_9",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-storage-dbspace/wooomtest-storage-dbspace_9/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-st-sc-wooom-stack4-prd-1.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-storage-dbspace_9",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28100
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-storage-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-storage-dbspace/wooomtest-storage-dbspace_10",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-storage-dbspace/wooomtest-storage-dbspace_10/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-st-sc-wooom-stack4-prd-2.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-storage-dbspace_10",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28100
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-storage-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-storage-dbspace/wooomtest-storage-dbspace_11",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-storage-dbspace/wooomtest-storage-dbspace_11/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "disabled": false,
  "hostname": "node-z5-wooomtest-st-sc-wooom-stack4-prd-3.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-storage-dbspace_11",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28400
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-support-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-support-dbspace/wooomtest-support-dbspace_12",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-support-dbspace/wooomtest-support-dbspace_12/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "hostname": "node-z5-wooomtest-sup-sc-wooom-stack4-prd-1.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-support-dbspace_12",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28400
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-support-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-support-dbspace/wooomtest-support-dbspace_13",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-support-dbspace/wooomtest-support-dbspace_13/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "hostname": "node-z5-wooomtest-sup-sc-wooom-stack4-prd-2.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-support-dbspace_13",
  "processType": "mongod",
  "version": "3.0.7"
}
{
  "args2_6": {
    "net": {
      "port": 28400
    },
    "operationProfiling": {},
    "replication": {
      "replSetName": "wooomtest-support-dbspace"
    },
    "storage": {
      "dbPath": "/data/wooomtest-support-dbspace/wooomtest-support-dbspace_14",
      "engine": "wiredTiger"
    },
    "systemLog": {
      "destination": "file",
      "path": "/data/wooomtest-support-dbspace/wooomtest-support-dbspace_14/mongodb.log"
    }
  },
  "authSchemaVersion": 5,
  "hostname": "node-z5-wooomtest-sup-sc-wooom-stack4-prd-3.sc-wooom-stack4-prd.local",
  "logRotate": {
    "sizeThresholdMB": 1000,
    "timeThresholdHrs": 24
  },
  "name": "wooomtest-support-dbspace_14",
  "processType": "mongod",
  "version": "3.0.7"
}

我只能提取版本

$ cat automationConfig |  jq '.processes[] .version' 
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"
"3.0.7"

现在我希望使用shell工具jq将数字“3.0.7”替换为“3.0.6”。

我关注此docs.mongodb.com,但希望降级(不升级)。

2 个答案:

答案 0 :(得分:3)

搜索包含您要更新的版本的进程,然后更新它们。

mLayoutManager.scrollToPositionWithOffset(position, 0)

答案 1 :(得分:1)

现在您已经提取了所需的值,只需要为它们分配新值即可。

Debian GNU/Linux 8