2016-09-26 29 views
-2

我有一个制表符分隔的文件中的客户列表,每个文件都有一组相应的IP范围。但是,我将它们上载到不接受第三个八位字节范围的数据库。也就是说,24.53.241.150-185的IP是可以接受的,但是24.53.150-185.241不是。单独的IP范围到单独的IP

为了将这个列表上传到数据库,我需要将第三个八位字节中的IP与第三个八位字节中的范围分开成单独的IP,而不在第三个字节中包含范围(例如,24.53.151.241,24.53.152.241等) ,并以相同的格式与“客户”和“管理员电子邮件”的相应字段进行匹配。

我该怎么做,使用任何工具将工作?我很灵活(Excel工具,正则表达式,Ruby等)。

什么,我目前拥有的格式: enter image description here

什么,这需要变成(与分隔成不同行的第三个八位字节):

enter image description here

+0

需要更多细节。你可以在数据库中添加任何你想要的列吗?还是你不得不使用它现有的格式?你到目前为止还尝试过什么吗? – Beartech

+0

你也可以说,不是像第2行那样的单个条目,你可以有75个条目都有'哈佛| [email protected] | 34.24.123。*'每个IP的线路从.123到.198?您需要提供数据在数据库中的外观示例。 – Beartech

+0

最后但并非最不重要,你有一些八位字节的'*'。这是否意味着你需要每一个0-255的条目? – Beartech

回答

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

这是一种将字符串转换为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"]