In order to check how many IPs are connected to my server I use the command as follows.

sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr

Display unique IP's in second column and the number of times this IP solicited Nginx, like :


I want to have a third column with the localization of this IP. For this I use geoiplookup.

Example of output :

23 Netherlands
3 England
1 Netherlands

Examples of geoiplookup output :

GeoIP Country Edition: US, United States
GeoIP Country Edition: FR, France

So a split at ", " is enough.

How can I transform the precedent output in order to have the localization on third column in one line ?

migrated from serverfault.com Jan 26 '17 at 19:54

This question came from our site for system and network administrators.

  • Please paste an example of what you'd want the single line output to appear as. – steve Jan 26 '17 at 20:08
  • how does the output of geoiplookup looks like (sample)? – George Vasiliou Jan 26 '17 at 22:41
  • I updated the answer – Totem Jan 26 '17 at 23:11
  • Do you mean that the code / one-liner you look for, has to perform geoiplookup to the ips that your existed command returns or the geoiplookup has already done by separate process and there is a kind of geoip log file and all you need is just to pull data out of this log? – George Vasiliou Jan 27 '17 at 16:02
  • Yes, but it's more a bash question. The command I put in my post have an interesting form and I wanted to know if we can transform it in order to make a 3rd column. Pure theoric question. – Totem Jan 27 '17 at 16:21
sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | awk '{"geoiplookup "$2" | egrep -o Country.*[A-Z]{2}" | getline line; split(line,a," "); print $1, a[3], $2}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.