Get frequency distribution of countries in a list of newline separated IP addresses in Linux

Assuming that the IP addresses are in a file called list.txt in a format such as below:

1
2
3
4
5
6
7
8
9
104.28.29.66
92.40.175.51
116.255.32.201
91.150.51.170
108.167.20.103
5.187.159.175
166.198.250.121
74.78.184.213
...

A prerequisite for this is the geoiplookup command, which is available in the geoip-bin package on most distros.

Run the following command to get the frequency distribution:

1
cat list.txt | xargs -n 1 geoiplookup | sed 's/GeoIP Country Edition: //g' | sort | uniq -c | sort -r

This will give you an output as:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    462 US, United States
    124 DE, Germany
     62 CA, Canada
     47 GB, United Kingdom
     34 AU, Australia
     31 NL, Netherlands
     23 FR, France
     19 IN, India
     18 SE, Sweden
     14 FI, Finland
     10 BE, Belgium
      9 NO, Norway
      9 IT, Italy
      9 BR, Brazil
      7 SG, Singapore
      7 PT, Portugal
      7 IL, Israel
      7 CH, Switzerland
      7 AT, Austria
      6 IR, Iran, Islamic Republic of

Read Next

I’m running an experiment for better content recommendations. These are the 3 posts that are most likely to be interesting for you:

  • Get Docker Containers running in a particular network
    Unlock the power of your network by learning how to effortlessly identify and organize running Docker containers, just like you mastered mapping IP addresses to countries.

  • Scraping the MPB website - A Postmortem
    Uncover the trials and triumphs of data extraction as you journey from mapping IP geolocations to wrestling with web scraping challenges, learning from real-world tech adventures.

  • 3 ways to render inline lists in PHP
    Unravel the art of data manipulation further by exploring how PHP can streamline your list processing tasks, just as you’ve mastered geographical data analysis with Linux commands.

All content is licensed under CC BY-NC-SA 4.0. Copying is an act of love - please copy!
More cool websites: Prev | Hotline Webring | Next
Built with Hugo
Theme Stack designed by Jimmy