0

we have the following 100-1000 files under /var/place folder

-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-10.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-11.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-12.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-13.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-14.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-15.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-16.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-17.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-18.json
-rw-r--r-- 1 root root   261 Dec 24 22:52 total_machines-19.json

. .

each file ( total_machines_rhel-xx.json ) look like this ( while topic value could be diff )

{
  "version": 1,
  "partitions": [
    {
      "topic": "total_machines_rhel",
      "partition": 10,
      "replicas": [
        1001,
        1003,
        1004
      ],
      "log_dirs": [
        "any",
        "any",
        "any"
      ]
    }
  ]
}

how to print the topic value name for all this files

expected results

total_machines-10.json topic=total_machines_rhel
total_machines-11.json topic=total_machines_fedora
total_machines-12.json topic=total_machines_aix
.
.
.

1 Answer 1

1

With jq version 1.5+

jq -r '.partitions[] | "\(input_filename) topic=\(.topic)"' total_machines-*.json

With Perl's JSON module:

perl -MJSON -0777 -nE '
  $h = decode_json($_); say "$ARGV topic=$h->{partitions}[0]{topic}"
' total_machines-*.json

With Miller, using an out-of-stream array indexed by the current filename. Note that Miller currently flattens JSON arrays into integer-keyed maps, but since your files only have a single element in each partitions array that's not a serious limitation in this case - it's really no worse than relying on the data being in element [0] in the perl version.

mlr --ijson --onidx put -S -q '
  @value[FILENAME] = "topic=".${partitions:0:topic}; end {emit @value, "a"}
' total_machines-*.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.