如何从json文件中捕获属性标签

如何从json文件中捕获属性标签

这是我的 json 文件的简短列表:

  "slider-client" : {
    "properties_attributes" : { },
    "properties" : { }
  }
},
{
  "spark2-hive-site-override" : {
    "properties_attributes" : { },
    "properties" : {
      "hive.metastore.client.connect.retry.delay" : "5",
      "hive.server2.enable.doAs" : "false",
      "hive.server2.thrift.port" : "10016",
      "hive.server2.transport.mode" : "binary",
      "hive.metastore.client.socket.timeout" : "1800"
    }
  }
},
{
  "tez-env" : {
    "properties_attributes" : { },
    "properties" : {
      "heap_dump_location" : "/tmp",
      "content" : "\n# Tez specific configuration\nexport TEZ_CONF_DIR={{config_dir}}\n\n# Set HADOOP_HOME to point to a specific hadoop install directory\nexport HADOOP_HOME=${HADOOP_HOME:-{{hadoop_home}}}\n\n# The java implementation to use.\nexport JAVA_HOME={{java64_home}}",
      "enable_heap_dump" : "false",
      "tez_user" : "tez"
    }
  }
},

我们如何只捕获以以下内容开头的行:

"properties" : {

并结束于

 }

预期输出示例

    "properties" : { }
    "properties" : {
      "hive.metastore.client.connect.retry.delay" : "5",
      "hive.server2.enable.doAs" : "false",
      "hive.server2.thrift.port" : "10016",
      "hive.server2.transport.mode" : "binary",
      "hive.metastore.client.socket.timeout" : "1800"
    }

    "properties" : {
      "heap_dump_location" : "/tmp",
      "content" : "\n# Tez specific configuration\nexport TEZ_CONF_DIR={{config_dir}}\n\n# Set HADOOP_HOME to point to a specific hadoop install directory\nexport HADOOP_HOME=${HADOOP_HOME:-{{hadoop_home}}}\n\n# The java implementation to use.\nexport JAVA_HOME={{java64_home}}",
      "enable_heap_dump" : "false",
      "tez_user" : "tez"
    }

到目前为止我所做的是这种语法,这是最好的方法吗?

 awk '/"properties" : {/,/^[[:blank:]]*}$/'  file.json

答案1

要成为有效的 JSON,您的输入应被视为对象数组。

杰克解决方案:

jq '.[] | to_entries[] | .value | if has("properties") then {"properties": .properties} else empty end' yourfile

输出(已更正):

{
  "properties": {}
}
{
  "properties": {
    "hive.metastore.client.connect.retry.delay": "5",
    "hive.server2.enable.doAs": "false",
    "hive.server2.thrift.port": "10016",
    "hive.server2.transport.mode": "binary",
    "hive.metastore.client.socket.timeout": "1800"
  }
}
{
  "properties": {
    "heap_dump_location": "/tmp",
    "content": "\n# Tez specific configuration\nexport TEZ_CONF_DIR={{config_dir}}\n\n# Set HADOOP_HOME to point to a specific hadoop install directory\nexport HADOOP_HOME=${HADOOP_HOME:-{{hadoop_home}}}\n\n# The java implementation to use.\nexport JAVA_HOME={{java64_home}}",
    "enable_heap_dump": "false",
    "tez_user": "tez"
  }
}

答案2

要捕获“}”之前的属性,我们可以这样做:

awk '/"properties" : {/,/^[[:blank:]]*}$/'  file.json

相关内容