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:
104.28.29.6692.40.175.51116.255.32.20191.150.51.170108.167.20.1035.187.159.175166.198.250.12174.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:
cat list.txt | xargs -n 1 geoiplookup | sed 's/GeoIP Country Edition: //g' | sort | uniq -c | sort -rThis will give you an output as:
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 ofThis work is licensed under CC BY-NC-SA 4.0. Copying is an act of love — please copy!