2012-03-18 68 views
0

我試圖開發一小段代碼,這將幫助我劃定一個日誌文件,該日誌文件已將大量跟蹤路由存儲到一週內發送的不同IP。將日誌文件解析爲CSV格式

我希望能夠在每個跟蹤路線的開始和結束處使用'--- START ---'和'--- END ---'標記將它們分開。例子如下:

--- START --- 
Mon Mar 12 22:45:05 GMT 2012 
traceroute to xxxxxxx (xxxxxxxx), 30 hops max, 60 byte packets 
1 xxxxxxx (xxxxx) 1.085 ms 1.662 ms 2.244 ms 
2 xxxxxxxx (xxxxxxx) 0.792 ms 0.782 ms 0.772 ms 
3 xxxxxxxxx (xxxxxxxxx) 8.545 ms 9.170 ms 9.644 ms 
4 etc 
5 etc 
--- END --- 
--- START --- 
Mon Mar 12 22:45:05 GMT 2012 
traceroute to xxxxxxxxx (xxxxxxxx), 30 hops max, 60 byte packets 
1 139.222.0.1 (xxxxxxxxx) 0.925 ms 1.318 ms 1.954 ms 
2 10.0.0.1 (xxxxxxxx) 0.345 ms 0.438 ms 0.496 ms 
3 172.16.0.34 (xxxxxxxxx) 0.830 ms 2.553 ms 0.809 ms 
4 etc 
5 etc 
6 etc 
--- END --- 

誰能給我一隻手伸進如何做到這一點做,在MATLAB或Java ......我也想算每一跳的路由跟蹤,使量。這是

乾杯--- END ---標籤拋出之前給出的數字...

任何幫助將不勝感激。

+2

只要看看每一行的第一個字母。如果它在CAPS中:它是第一個包含日期的行。如果它是小寫字母(traceroute),它是第二行。如果它是數字:它是「跳躍」線之一。 – wildplasser 2012-03-18 21:35:04

回答

2

快速「N髒例如:

import java.io.*; 
import java.text.*; 
import java.util.*; 
import java.util.regex.*; 

public class Main 
{ 
    private static class Record 
    { 
    public Date date; 

    public String to; 
    public int hops; 
    public int packetSize; 

    public String toString() 
    { 
     return date + " ::: " + to + ", " + hops + " hops, " 
     + packetSize + " bytes"; 
    } 
    } 

    public static void main(String[] args) throws Exception 
    { 
    Scanner s = new Scanner(new FileReader(new File("input.txt"))); 

    Record currentRecord = null; 
    ArrayList<Record> list = new ArrayList<Record>(); 

    while (s.hasNextLine()) { 
     String line = s.nextLine(); 

     if ("--- START ---".equals(line)) { 
     currentRecord = new Record(); 

     } else if ("--- END ---".equals(line)) { 
     list.add(currentRecord); 
     currentRecord = null; 

     } else if (currentRecord.date == null) { 
     currentRecord.date = new SimpleDateFormat(
      "EEE MMM dd HH:mm:ss zzz yyyy").parse(line); 

     } else if (line.startsWith("traceroute to ")) { 
     Pattern p = Pattern.compile(
      "traceroute to ([^ ]+) [^,]+, ([^ ]+) hops max, ([^ ]+) byte packets"); 
     Matcher m = p.matcher(line); 

     if (m.matches()) { 
      currentRecord.to = m.group(1); 
      currentRecord.hops = Integer.parseInt(m.group(2)); 
      currentRecord.packetSize = Integer.parseInt(m.group(3)); 
     } 
     } 
    } 

    for (int i = 0; i < list.size(); i++) 
     System.out.println(list.get(i).toString()); 
    } 

} 

輸出:

Tue Mar 13 04:15:05 GMT+05:30 2012 ::: 203.98.69.105, 30 hops, 60 bytes 
Tue Mar 13 04:15:05 GMT+05:30 2012 ::: 62.193.36.27, 30 hops, 60 bytes 

我指點你在一堆不同的方向(ScannerPatternSimpleDateFormat等)。對於單個「網關」項目,您還可以使用String.split(),使用" "(兩個空格)作爲分隔符。

+0

感謝您的幫助。我也試圖顯示每個跟蹤路由的跳數,我發現它很難,因爲它沒有聲明最後的跟蹤路由。而是顯示'--- END ---'行之前的最後一條曲線。 我該如何去告訴代碼在最後END行之前找到最後一條軌跡?然後id能夠打印它製作的啤酒花的數量... – FredBones 2012-03-19 13:12:38

+0

@FredBones正如我在帖子中所說的,我已經通過向您介紹一堆您可以用來解析的類指出了正確的方向文本。提示:添加另一個「else if」塊並解析跳躍,包括數字(首先出現!);用包含所有跳躍的'ArrayList'更新'Record'。我希望你能夠自己寫出邏輯。 – Manish 2012-03-19 17:50:54

+0

Manish,感謝幫助!我在一段時間內習慣性地使用了java,並且不得不跳回到它,所以發現它有點困難。我接受你的建議,並告訴你它有多好!乾杯! – FredBones 2012-03-19 23:08:51