2016-09-26 21 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"]