我在制表符分隔文件中有一个客户列表,每个文件都有一组相应的IP范围。但是,我将它们上传到不接受第三个八位字节范围的数据库。也就是说,IP为24.53.241.150-185是可以接受的,但24.53.150-185.241不是。
为了将此列表上传到数据库,我需要将第三个八位字节中的范围的IP分成不同的IP,而不包括第三个八位字节中的范围(例如,24.53.151.241,24.53.152.241等),并将其匹配为与#34; Customer"的相应字段相同的格式。和"管理员电子邮件"。
我怎么能这样做,使用什么工具?我很灵活(Excel工具,Regex,Ruby等)。
需要变成什么(第三个八位字节分成不同的行):
答案 0 :(得分:0)
我已经使用Ruby代码解决了这个问题,并在将Excel文件保存为制表符分隔的TXT文件后应用了它。
def expand_lines(line)
id, inst, ip = line.split("\t")
ip_compontents = ip.split('.')
if ip_compontents[2] =~ /(\d+)-(\d+)/
$1.to_i.upto($2.to_i).map do |i|
new_ip = [*ip_compontents[0..1], i, ip_compontents[3]].join('.')
[id, inst, new_ip]
end
else
[[id, inst, ip]]
end
end
if $0 == __FILE__
ext = File.extname(ARGV[0])
base = File.basename(ARGV[0], ext)
dir = File.dirname(ARGV[0])
outfile = File.join(dir, "#{base}_expanded#{ext}")
expanded = IO.read(ARGV[0]).split("\n").map {|l| expand_lines(l.chomp)}.flatten(1)
File.open(outfile, 'w') do |f|
f.puts expanded.map {|l| l.join("\t")}
end
end
答案 1 :(得分:-1)
这是将字符串转换为ips范围的一种方法。
def convert_to_range(ip)
arr = ip.split('.')
ndx = arr.index { |s| s =~ /-/ }
f,l = arr[ndx].split('-')
(f..l).map { |s| [*arr.first(ndx), s, *arr[ndx+1..-1]].join('.') }
end
convert_to_range("24.53.94-105.241")
#=> ["24.53.94.241", "24.53.95.241", "24.53.96.241", "24.53.97.241",
# "24.53.98.241", "24.53.99.241", "24.53.100.241", "24.53.101.241",
# "24.53.102.241", "24.53.103.241", "24.53.104.241", "24.53.105.241"]
convert_to_range("24.53-58.105.241")
#=> ["24.53.105.241", "24.54.105.241", "24.55.105.241", "24.56.105.241",
# "24.57.105.241", "24.58.105.241"]
convert_to_range("24-26.58.105.241")
#=> ["24.58.105.241", "25.58.105.241", "26.58.105.241"]
convert_to_range("26.58.105.241-248")
#=> ["26.58.105.241", "26.58.105.242", "26.58.105.243", "26.58.105.244",
# "26.58.105.245", "26.58.105.246", "26.58.105.247", "26.58.105.248"]