I would like to automatize a process.

I have a list of IP addresses to use in a curl request:

curl http://api.geoiplookup.net/?query=($ip)

From curl's output, I want to grab the city and write a line to a text file with the format IP_address:city.

In order to grep the city I found this (google example):

curl http://api.geoiplookup.net/?query= | grep -oP '(?<=\<city\>).*(?=\<\/city\>)'

To automatize the process I did (but need to improve ^^):

for ip in $(cat essai); do curl http://api.geoiplookup.net/?query=$ip & done

When I try to add my grep command, it doesn't show me the city and gives an error.

Thanks a lot.

  • Welcome to the site. To make it easier for contributors to help you, please edit your post to include the exact output and error message you get, using proper formatting.
    – AdminBee
    Commented May 19, 2020 at 8:15
  • The (...) brackets in your example are wrong. Perhaps you meant curl "http://api.geoiplookup.net/?query=$ip" Commented May 19, 2020 at 21:12

1 Answer 1


First of all, always quote URLs given on the command line. Some URLs may contain the character &, which will be interpreted by the shell as if you wanted to start a background job. Some URLs, like the one you show, contains filename globbing characters, and some shells, depending on their configuration, will therefore try to match them against the files in the current directory, and may fail with an error if there is no match:

$ curl http://api.geoiplookup.net/?query=
zsh: no matches found: http://api.geoiplookup.net/?query=

To parse out the IP address and the city from the XML document in the response, use xmlstarlet:

$ curl -s "http://api.geoiplookup.net/?query=$ipaddr" | xmlstarlet sel -t -m '//result' -v 'concat(host, ":", city)' -nl View

To loop over all IP addresses in the file essai, assuming there is one address per line:

while IFS= read -r ipaddr; do
    curl -s "http://api.geoiplookup.net/?query=$ipaddr" |
    xmlstarlet sel -t -m '//result' -v 'concat(host, ":", city)' -nl
done <essai

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .