2015-09-06 54 views
1

我有這個提供程序ip地址範圍的巨大列表,其中包含有關地址總數和提供程序名稱的其他信息。
它幾乎是這個樣子的:從csv文件中提取數據並保存到文本文件中(不會導致RAM過載)

2.160.0.0,2.175.255.255,1048576,28/09/10,Telekom Deutschland GmbH 
2.200.0.0,2.207.255.255,524288,18/11/10,Vodafone GmbH 

回家喂到另一個程序,我不得不將其轉換爲一個包含IP範圍的基本清單的簡單文本文件。就像這樣:

2.160.0.0-2.175.255.255 
2.200.0.0-2.207.255.255 

如此以來,文件確實非常大的問題是:
我如何轉換這種CSV表到基於TXT IP範圍列表,而整個文件加載到內存在同一時間?

回答

0

嗯,這是我想出的答案。純AutoHotkey並且一次只能將一條線加載到RAM中,並且它實際上非常快速且可靠。

;Set input and output files 
inputFile := "log.csv" 
outputFile := "out.txt" 

;If the output file exists already, delete it before we start. 
If FileExist(outputFile) { 
    FileDelete, %outputFile% 
    If ErrorLevel 
     MsgBox, Error: Can't access "%outputFile%"! 
} 

;Count the lines of our input file 
Loop, Read, %inputFile% 
    lineCount := A_Index 

;Parse the input file, filter it and output it to the output file 
Loop, Read, %inputFile%, %outputFile% 
{ 
    currentLine := A_LoopReadLine 
    If (currentLine != "") { ;If the current line is not empty 
     currentLineArray := StrSplit(currentLine,",") ;Split the line by the comma 
     stringToWrite := currentLineArray[1] "-" currentLineArray[2] . "`r`n" ;generate a basic ip range line with a dash as a seperator 
     FileAppend, %stringToWrite% ;write it to our output file 
     ;TrayTip, Writing to ip range file..., Line %A_Index%/%lineCount% 
    } 
} 
相關問題