#!/usr/bin/perl use strict; use warnings; use Set::CrossProduct; while () { chomp; s/\s*#.+//; my @data; while (/([^.]+)/g) { if ($1 =~ /(\d+)-(\d+)/) { push @data, [$1..$2]; } else { push @data, [$1]; } } for (Set::CrossProduct->new( \@data )->combinations) { print join(".", @$_), "\n"; } } __DATA__ 172.17.119.2 # Comments are here... 172.17.119.4-5 # Comments are here... 172.19-21.254.2-3 # Comments are here... 192.168.1.1-3 # Comments are here...