Vetal
Vetal

Reputation: 275

jq, call another command inline

I would like to call another command inside of jq query.

For example,

{
   hostA: "somedomain.com",
   hostB: "anotherdomain.com"
}

I need to transform to:

{
   hostA: "123.123.123.123",
   hostB: "132.132.132.132"
}

Applying this command on every value: dig +short $value

So, each host is transformed to IP.

Or, bash, iterate over result, reconstruct json only?

If so, what is the easiest/cleanest solution? I see it as I have to recreate second, mapping json and apply solutions from jq, lookup 1 or jq, lookup 2. Still, looks like a brute force solution.

Upvotes: 0

Views: 1295

Answers (1)

nir
nir

Reputation: 46

You can use bash arrays and iterate over it in a loop:

Took the answer from - https://stackoverflow.com/a/67638584/16822178

# read each item in the JSON array to an item in the Bash array
readarray -t host_array < <(jq -c '.[]' input.json)

# iterate through the Bash array
for host in "${host_array[@]}"; do
  dig +short "$host"
done

Upvotes: 1

Related Questions