I have a file with groups of IP addresses. The file looks like this:London:1.1.1.0-1.1.1.200172.25.2.0-172.25.2.100Germany:2.2.2.0-2.2.2.100192.168.1.0-192.168.1.200172.25.2.0-172.25.2.200So when I search for an IP address (./program.sh 172.25.2.32 ) the output should be London and Germany.
Finding groups of IP addresses in a file
bash;text processing
You haven't accepted the other solutions, maybe because their output doesn't match the requested London and GermanyModern Python can handle IP addresses out of the box:#! /usr/bin/env python3.4# coding: utf-8# also works on 2.7 after installing ipaddress from PyPIimport sysfrom ipaddress import ip_addressprintip_string = sys.argv[1]if sys.version_info < (3,):    ip_string = ip_string.decode('utf-8')ip = ip_address(ip_string)locs = []for line in open('input'):    try:        loc, rest = line.split(':')    except ValueError:        try:            if sys.version_info < (3,):                sr, er = map(ip_address, [x.decode('utf-8') for x in line.rstrip().split('-')])            else:                sr, er = map(ip_address, line.rstrip().split('-'))            if sr < ip < er:                locs.append(loc)        except ValueError:            print 'error in input line:', repr(line)            breakif len(locs) > 2:    locs = [', '.join(locs[:-1]), locs[-1]]print(' and '.join(locs))This prints the found locations and puts and between the last two locations found and , between all others (when applicable): e.g. London, Paris and GermanyThe data is assumed to be in a file called input, make the script executable, or call with python scriptname 172.25.2.32