0

I want to parsing JSON data using jq(as described here ) and delete any newlines character from the resulting string.

I've already tried to use tr but this approach remove also all the white spaces between parsed values.

My code:

IP=$(curl -s https://ipinfo.io/ip) # Get ip address
curl -s https://ipinfo.io/${IP}/geo | jq -r '.ip, .city, .country' | tr -d '\n' # parse only few values from the JSON data and remove new lines.

What i get with the code above is the following string: XXX.XXX.XXX.XXXCity_NameCountry_Name but i want something like this: XXX.XXX.XXX.XXX City_Name Country_Name

beep
  • 1,057
  • 2
  • 11
  • 23

1 Answers1

0

You could craft a single string from the three pieces of data so that it appears on a single line (single result by input) :

IP=$(curl -s https://ipinfo.io/ip)
curl -s https://ipinfo.io/${IP}/geo | jq -r '.ip + " " + .city + " " + .country'
> myIp myCity myCountryCode

Another option for a different but similar output format would be to use the @csv output format, where you would want to output an array of cells for each input :

IP=$(curl -s https://ipinfo.io/ip)
curl -s https://ipinfo.io/${IP}/geo | jq -r '[.ip, .city, .country] | @csv'
> "myIp","myCity","myCountryCode"

This result could be easily worked on from any spreadsheet software.

Aaron
  • 24,009
  • 2
  • 33
  • 57