0

I want to iterate in bash over this json string (generated with jq) :

{
  "hostname": "Arthhur",
  "long": 10.234656,
  "lat": 54.3454345
}
{
  "hostname": "Zaphod",
  "long": 10.12344324,
  "lat": 54.35672134
}
...

to generate zipcodes from the location using:

    plz=`wget -qO - "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=true"|grep '"[0-9][0-9][0-9][0-9][0-9]"'|head -n1`

    plz=${plz##*\ :\ \"}
    plz=${plz%%\"*}

How can I pipe the string into a bash loop, so I can call that wget command for each hostname, having two variables inside the loop for long and lat ?

rubo77
  • 19,527
  • 31
  • 134
  • 226
  • I'm not sure what you get back from your initial `wget` call, but presuming it is the json string, just use `while read -r your vars ...; do ... your parsing ...; done < <($(wget -qO - "http://maps.googleapis.com....))` That is just standard *process substitution* if I understood your question. – David C. Rankin Sep 15 '15 at 02:11
  • https://github.com/dominictarr/JSON.sh is a JSON parser in shell. – chicks Sep 15 '15 at 04:53
  • Not really sure why the geocoding api / zip part matters here? [How to parse json in bash](http://stackoverflow.com/questions/1955505/parsing-json-with-sed-and-awk) – Reinstate Monica Please Sep 15 '15 at 06:22
  • I know how to parse the json string with `jq` which works fine. My problem is how do I get the output into the while loop with one json object per loop. I need one variable for long and one for lat in each loopg – rubo77 Sep 15 '15 at 12:21
  • http://stackoverflow.com/questions/1955505/parsing-json-with-sed-and-awk does not answer my question at all. Because I already use `jq` to parser a much bigger json which does not matter here – rubo77 Sep 15 '15 at 12:25
  • @rubo77 Why not just parse out the parts you want [with jq](http://unix.stackexchange.com/questions/177843/parse-one-field-from-an-json-array-into-bash-array) – Reinstate Monica Please Sep 15 '15 at 23:04
  • I think my main problem is, that i want five lines of the output in one iteration of the loop – rubo77 Sep 16 '15 at 05:07
  • 1
    @rubo77 That shouldn't be a problem you can replace the `echo` below with whichever lines. If you you're getting the string from a command rather than a file, you can also just use process substitution instead, i.e. `done < <(your command that generates string)` below in place of the file. – Reinstate Monica Please Sep 16 '15 at 10:30

1 Answers1

1

Still a bit confused why you parsed the original json into something you still need to parse, but something like this should generally work for the specific string

#!/bin/bash
unset lat long hostname
while IFS= read -r line; do
  [[ $line =~ '"hostname": "'([^\"]+) ]] && hostname=${BASH_REMATCH[1]}
  [[ $line =~ '"long": '([^,[:space:]]+) ]] && long=${BASH_REMATCH[1]}
  [[ $line =~ '"lat": '([^,[:space:]]+) ]] && lat=${BASH_REMATCH[1]}
  if [[ -n $hostname && -n $long && -n $lat ]]; then
    echo "Hostname=[$hostname], Lat=[$lat], Long=[$long]" #or whatever else with them
    unset lat long hostname
  fi
done < file

i.e.

> cat file
{
  "hostname": "Arthhur",
  "long": 10.234656,
  "lat": 54.3454345
}
{
  "hostname": "Zaphod",
  "long": 10.12344324,
  "lat": 54.35672134
}
> ./abovescript
Hostname=[Arthhur], Lat=[54.3454345], Long=[10.234656]
Hostname=[Zaphod], Lat=[54.35672134], Long=[10.12344324]
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48