-3

this is very short list of my json file:

  "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"
    }
  }
},

how we can capture only the lines that start with:

"properties" : {

and ended with

 }

example of expected output

    "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"
    }

what I did until now is this syntax , is it the best approach ?

 awk '/"properties" : {/,/^[[:blank:]]*}$/'  file.json
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
jango
  • 403
  • 2
  • 13
  • 18
  • 3
    Your JSON excerpt is not valid JSON, important parts are missing. The simplest way to do what you want is use to use `jq`, but without knowing the exact JSON format, we can't make a ready-to-use answer. Have you tried reading the `jq` man page, and attempting it yourself? – dirkt Aug 16 '17 at 07:21
  • yes I read the jq but after few tests I cant to find the right way – jango Aug 16 '17 at 07:27
  • maybe awk that runs on the file – jango Aug 16 '17 at 07:32
  • There are plenty of questions here about parsing a JSON file and sensible answers will always be based on a JSON parser, not on `sed`/`awk`. Unless of course you have full control on the JSON output and thus can avoid all kind of side-effects, which you didn't state in your question. – xhienne Aug 17 '17 at 12:21

2 Answers2

1

To be a valid JSON your input should be considered as an array of objects.

jq solution:

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

The output (corrected):

{
  "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"
  }
}
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
-1

to capture the properties until "}" we can do:

awk '/"properties" : {/,/^[[:blank:]]*}$/'  file.json
jango
  • 403
  • 2
  • 13
  • 18