IO.foreach('ips.txt') do |line| match = line.sub(/^(\d+\.\d+\.\d+)\.\d+$/, '\1').chomp for i in 0..255 print match, ".#{i}\n" end end