I am saving the information of a curl in a variable. The output is as follows:
INFOISP=$(curl --silent ipinfo.io | grep -E '"ip"|"hostname"|"region"|"country"|"org"')
The output is this:
"ip": "186.152.174.190",
"hostname": "host190.186-152-174.telecom.net.ar",
"region": "Santa Fe",
"country": "AR",
"org": "AS7303 Telecom Argentina S.A.",
I need this:
"ip": "186.152.174.190"
"hostname": "host190.186-152-174.telecom.net.ar"
"region": "Santa Fe"
"country": "AR"
"org": "AS7303 Telecom Argentina S.A."
I try with sed using this:
INFOISP=$(curl --silent ipinfo.io | grep -E '"ip"|"hostname"|"region"|"country"|"org"')
echo ${INFOISP} | sed -e $'s/,/\\\n/g'
but the output is not in the same column:
"ip": "186.152.174.190"
"hostname": "host190.186-152-174.telecom.net.ar"
"region": "Santa Fe"
"country": "AR"
"org": "AS7303 Telecom Argentina S.A."
any helps?
CodePudding user response:
The output of your curl
command is a well formatted JSON so you could use jq
for extracting data from it robustly. For what you're trying to do you can use other tools though, for ex. GNU grep
:
curl --silent ipinfo.io |
grep -oE '("ip"|"hostname"|"region"|"country"|"org"): "[^"]*"'
CodePudding user response:
You could do the filtering within sed
skipping the need for grep
$ infoisp=$(curl --silent ipinfo.io | sed -En '/ip|hostname|region|country|org/{s/[^"]*([^,]*).*/\1/p}')
$ echo "$infoisp"
"ip": "186.152.174.190"
"hostname": "host190.186-152-174.telecom.net.ar"
"region": "Santa Fe"
"country": "AR"
"org": "AS7303 Telecom Argentina S.A."